[英]Sql table join with no direct relation [ codeigniter ]
我想加入其中,如表table1
和table2
是常見的領域和table2
包含字段record_id
衛生組織的描述是在table3
場record_desc
什么是查詢?
table1
和table3
之間沒有直接關系
我試過這樣但是不行。
$this->db->select('*');
$this->db->from('table1');
$this->db->join('table2', 'table1.id = table2.id');
$this->db->join('table3', 'table2.record_id= table3.record_desc');
$query = $this->db->get();
我認為這條線是不正確的
$this->db->join('table3', 'table2.record_id= table3.record_desc');
因為table3.record_desc
不是從表2到表3的外鍵嗎?
查詢可能是這樣的
$this->db->join('table3', 'table2.record_id= table3.record_id');
因為你必須有一個外鍵來匹配表2和表3中的記錄。
當你選擇結果時,如果你這樣做
$this->db->select('*');
如果每個表中具有相同的列名,則結果中只會顯示一列。
所以盡量只獲得你想要的列
$this->db->select('table1.id,table2.record_id,table3.record_desc');
希望這可以幫助 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.