2013-06-26 46 views
0

我得到了不同的結果,當我在codeigniter活動記錄的形式執行查詢MyPhpAdmin笨的Active Record響應

使用MyPhpAdmin

SELECT * FROM (`ingredients`) LEFT JOIN `product_ingredients` ON `ingredients`.`ingredient_id` = `product_ingredients`.`ingredient_id` AND `product_id` = 46 

下面的查詢運行提供了所需的結果是其中包括返回的兩種成分記錄的ingredient_id。

Array ([0] => stdClass Object ([ingredient_id] => 1 [display] => Sweet Almond Oil [slug] => sweet-almond-oil [title] => Sweet Almond Oil [description] => Sweet Almond Oil [featured_image_url] => [product_ingredient_id] => 10 [product_id] => 46 [display_position] => 2 [key_ingredient] => 0) [1] => stdClass Object ([ingredient_id] => 2 [display] => Shea Butter [slug] => shea-butter [title] => Shea Butter [description] => Shea Butter [featured_image_url] => [product_ingredient_id] => [product_id] => [display_position] => [key_ingredient] =>)) 

但是,如果我執行使用Codeigniter活動記錄用下面的代碼此查詢:

$this->db->select('*'); 
$this->db->join('product_ingredients', 'ingredients.ingredient_id = product_ingredients.ingredient_id AND `product_id` = 46', 'left'); 
$query = $this->db->get('ingredients'); 

然後將所得陣列缺少第二成分結果其是ingredient_id:

Array ([0] => stdClass Object ([ingredient_id] => 1 [display] => Sweet Almond Oil [slug] => sweet-almond-oil [title] => Sweet Almond Oil [description] => Sweet Almond Oil [featured_image_url] => [product_ingredient_id] => 10 [product_id] => 46 [display_position] => 2 [key_ingredient] => 0) [1] => stdClass Object ([ingredient_id] => [display] => Shea Butter [slug] => shea-butter [title] => Shea Butter [description] => Shea Butter [featured_image_url] => [product_ingredient_id] => [product_id] => [display_position] => [key_ingredient] =>)) 

任何幫助將非常感激...

回答

0

看看吧

$this->db->select('i.*'); 
$this->db->from('ingredients i'); 
$this->db->join('product_ingredients pi', 'i.ingredient_id = pi.ingredient_id', 'left'); 
$this->db->where('pi.product_id',46); 
$query = $this->db->get()->result_array();