我有3個表我想要加入,但是當我在第三個表上使用where語句,並且第三個表沒有它時,它不即使我正在使用左連接,也會返回第一個和第二個表中的行。CI MySQL查詢連接表和其中語句不返回所有行
Table 1
+---------+--------------+----------+
| acc_PID | acc_name | acc_type |
+---------+--------------+----------+
| 1 | Account 1 | 1 |
| 2 | Account 2 | 1 |
| 3 | Account 3 | 2 |
| 4 | Account 4 | 1 |
+---------+--------------+----------+
Table 2
+-------------+-----------------+-----------+
| journal_PID | journal_account | trans_PID |
+-------------+-----------------+-----------+
| 1 | 1 | 1 |
| 2 | 2 | 2 |
| 3 | 1 | 3 |
+-------------+-----------------+-----------+
Table 3
+-----------+----------------+
| trans_PID | trans_location |
+-----------+----------------+
| 1 | 1 |
| 2 | 1 |
| 3 | 2 |
+-----------+----------------+
// CI query
$this->db->join('table_2 b', 'a.acc_PID = b.journal_account', 'LEFT');
$this->db->join('table_3 c', 'b.trans_PID = c.trans_PID', 'LEFT');
$this->db->where('a.acc_type', '1');
$this->db->where('c.trans_location', '1');
$this->db->group_by('a.acc_PID');
$query = $this->db->get('table_1 a');
$result = $query->result();
現在從上面的數據,如果我使用($這 - > DB->其中( 'c.trans_location', '1')),結果不會因爲沒有返回帳戶4在表2和表3中,acc_PID ='4'的數據,但是我希望結果也返回帳戶4,即使表2和表3中沒有帳戶4的數據,也沒有$ this-> db-> where('c .trans_location','1'),結果也顯示了帳戶4,但是使用where位置語句它不會返回表1中的行,即使我使用了左連接,也不應該返回表1的結果嗎?
預先感謝您。
嘗試在連接中添加條件而不是where子句。 –
這可能會幫助你http://stackoverflow.com/questions/15992236/codeigniter-join-with-multiple-conditions –
@AmiteshKumar愚蠢的我!謝謝先生,請寫下您的答案,以便我可以接受它作爲答案。 – Charas