【发布时间】:2020-11-03 22:41:25
【问题描述】:
大家好,我是 codeigniter 4 的新手,目前正在处理项目中的一个小项目,我正在尝试连接三个表并在单个表中显示数据。
Table_1
id unique_id Name
1 1111 Sam
2 2222 Charlote
Table_2
id unique_id Name
1 1212 Jhon
2 5151 Alex
Table_3
id author title
1 1111 Book_1
2 5151 Book_2
3 1111 Book_3
Result
------------------------
| No | Author | Title |
------------------------
| 1 | Sam | Book_1 |
| 2 | Alex | Book_2 |
| 3 | Sam | Book_3 |
------------------------
我已尝试加入,但无法正常工作。
$this->join('Table_1', 'Table_1.unique_id = Table_3.author ');
$this->join('Table_2', 'Table_2.unique_id = Table_3.author ');
$this->select('Table_1.Name');
$this->select('Table_2.Name');
$this->select('Table_3.*');
$this->orderBy('Table_3.id');
return $this->findAll();
还有其他方法可以加入他们吗? 谢谢
【问题讨论】:
-
您似乎错过了选择数据,您是否阅读过此codeigniter.com/user_guide/database/…? $this->select(),只构建查询的SELECT子句
-
@Vickel 当我更改为 $this->select('*');加入后,我没有结果。但是当我删除连接线上的 table_1 时,我得到了结果,但只是针对 table_2。
-
如果您使用的是 MySQL,您将如何使用普通 SQL 来执行此操作?
标签: mysql codeigniter codeigniter-4