繁体   English   中英

Sql表连接没有直接关系[codeigniter]

[英]Sql table join with no direct relation [ codeigniter ]

我想加入其中,如表table1table2是常见的领域和table2包含字段record_id卫生组织的描述是在table3record_desc什么是查询?

table1table3之间没有直接关系

我试过这样但是不行。

$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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM