Sql table 没有直接关系加入 [codeigniter]
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
不是从 table 2 到 table 3 的外键,对吗?
查询可能是这样的
$this->db->join('table3', 'table2.record_id= table3.record_id');
因为你必须有一个外键来匹配 table 2 和 table 3 中的记录。
当您选择结果时,如果您这样做
$this->db->select('*');
如果每个 table 中的列名称相同,则结果中只会出现一列。
所以尝试像这样只获取您想要的列
$this->db->select('table1.id,table2.record_id,table3.record_desc');
希望这对您有所帮助。
我想连接表,其中 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
不是从 table 2 到 table 3 的外键,对吗?
查询可能是这样的
$this->db->join('table3', 'table2.record_id= table3.record_id');
因为你必须有一个外键来匹配 table 2 和 table 3 中的记录。
当您选择结果时,如果您这样做
$this->db->select('*');
如果每个 table 中的列名称相同,则结果中只会出现一列。
所以尝试像这样只获取您想要的列
$this->db->select('table1.id,table2.record_id,table3.record_desc');
希望这对您有所帮助。