2013-05-04 35 views
0

行爲不我看起來像這樣的兩個表:笨activerecords左加入如預期

Table_one 


P_id  Value 
1001  50 
1002  54 
1003  47 
1004  65 
1005  51 

Table_two 

S_id  Rating  t_id 
1001  1   1 
1002  3   1 
1001  5   2 
1004  1   2 
1005  2   2 

我想加入使用CodeIgniter的Active Record類,這兩個表。這裏是我的代碼:

$tId = 1; 
$this->db->select('Table_one.Value, Table_two.Rating'); 
$this->db->from('Table_one'); 
$this->db->join('Table_two', 'Table_one.P_id = Table_two.S_id', 'left'); 
$this->db->where('Table_two.t_id', $tId); 
$query = $this->db->get(); 

我期待此查詢提供所有5行數據從表1中的值追加到前兩行的「等級」表2列。相反,我只從前兩行獲取數據。我如何重寫代碼,繼續使用活動記錄類來獲得我的預期結果?

回答

0

您的$tid似乎是where條款中的1。這將導致僅檢索前兩行的值。

0

如果你想得到所有5行,你需要擺脫where子句,因爲Table_two中只有兩行有t_id = 1,這就是你所得到的。

0

通過看你的代碼它必須返回2行...如果你想檢索5行然後刪除where子句。