php - 使用 codeigniter 通过 OR 进行内连接

标签 php mysql codeigniter

我想将表 a 联接到表 b 或表 c

我有数据库

table a
id
email

table b
id
name
admin

table c
id
name
user
//ilustration
table b.id = 01-admin
table c.id = 01-user

如果表a.id = 01-user,我可以从表c获取数据 并且数据将在表 a 上循环

我尝试示例

$this->db->select('*');
$this->db->from('a');
$this->db->join('b', 'a.id = b.id');
//OR

$this->db->join('c', 'a.id = c.id');
$query = $this->db->get();
return $query;

请帮助我,
如何编写带有条件 OR 的 JOIN 代码

最佳答案

可能使用左外连接:

$this->db->select('*');
$this->db->from('a');
$this->db->join('b', 'a.id = b.id', 'LEFT OUTER');
$this->db->join('c', 'a.id = c.id' 'LEFT OUTER');
$query = $this->db->get();
return $query;

只有匹配时,您才会从 B 或 C 获得东西。

关于php - 使用 codeigniter 通过 OR 进行内连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42764197/

相关文章:

php - 通过php从textarea字段的每一行插入数据库

javascript - 从图表中的数据库中选择不更新值 php

mysql - MySQL表的分散关系结构

php - 在 codeigniter 的 form_multiselect() 中设置多个值

php - 方法声明应与 PHP 中的父方法兼容

php - Mysql on select update records selected

mysql - mysql中的数据透视表

Mysql Slave 不更新

php - CodeIgniter session 错误 - 不允许取消链接操作

php - num_rows() 和 result_array() 在同一个函数中?代码点火器