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

标签 mysql database codeigniter

我想连接表,其中 table1table2 是公共(public)字段,并且 table2 包含字段 record_id whos描述位于 table3 字段 record_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'); 

希望这有帮助。

关于mysql - 没有直接关系的 Sql 表连接 [ codeigniter ],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28893383/

相关文章:

mysql - 是否有允许我在 MySql 查询中使用周数的函数?

sql - 我们可以在一列上添加多个别名吗?

database - 实体教师和学生的共享表?

mysql - SQLSTATE[42000] : Syntax error or access violation: 1066 Not unique table/alias on relationship

mysql - 使用外键 : Getting a column from another table using the id column

php - 如何在 JSON 中为来自两个不同表的类别的选中 1 或 0 提供响应?

database - Codeigniter 在数据库中存储 session

codeigniter - 如何在Codeigniter迁移文件中添加小数字段?

php - CodeIgniter 大批量插入

php - 限制与 Laravel 的多态多对多关系中的相关记录