简体   繁体   English

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

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

I want to join tables where as table1 and table2 are common fields and table2 contain field record_id whos description is in table3 field record_desc what would be query? 我想加入其中,如表table1table2是常见的领域和table2包含字段record_id卫生组织的描述是在table3record_desc什么是查询?

There is no direct relation between table1 and table3 table1table3之间没有直接关系

I tried like this but won't work. 我试过这样但是不行。

$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();

I think this line is incorrect 我认为这条线是不正确的

$this->db->join('table3', 'table2.record_id= table3.record_desc');

because table3.record_desc is not the foreign key from table 2 to table 3 right ? 因为table3.record_desc不是从表2到表3的外键吗?

The query may be like this 查询可能是这样的

$this->db->join('table3', 'table2.record_id= table3.record_id');

because you have to have a foreign key to match records in table 2 and table 3 . 因为你必须有一个外键来匹配表2和表3中的记录。

and when you are selecting the result if you are doing like 当你选择结果时,如果你这样做

$this->db->select('*'); 

If you have same column name in each table , only one column will appear in the result . 如果每个表中具有相同的列名,则结果中只会显示一列。

so try to get only columns you want like this 所以尽量只获得你想要的列

$this->db->select('table1.id,table2.record_id,table3.record_desc'); 

Hope this helps . 希望这可以帮助 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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