【问题标题】:Joining on codeigniter tables in this query multi tables在此查询多表中加入 codeigniter 表
【发布时间】:2013-03-20 23:19:27
【问题描述】:

只想在 CODEIGNITER 中加入 1 到 3 个表,怎么做

在这个查询中,我只加入了一个表,我如何加入我的第三、第四个表

    $this->db->select('*');
    $this->db->from('table1');
    $this->db->join('table1', 'table1.dep_id = table2.dep_id', 'table1.status= 1');
    $this->db->limit(1);
    $this->db->order_by("expiredate", "desc");

    return $this->db->get()->result();

我的表结构如下

表1

t1_id
t1_name
t2_id
t3_id
t4_id

表2

t2_id
t2_name

表3

t3_id
t3_name

table4

t4_id
t4_name

我怎样才能加入我的第三和第四次

提前问好……

【问题讨论】:

标签: php mysql codeigniter


【解决方案1】:

试试同步加入

$this->db->join('table1', 'table1.t2_id = table2.t2_id', 'table1.status= 1');

同时你需要加入 table1 , tabl2 中的任何一个来加入 table3 或 4 之类的

$this->db->join('table3', 'table1.t3_id = table3.t3_id');
$this->db->join('table4', 'table1.t4_id = table4.t3_id');

而且你需要选择像这样的表

$this->db->select('table1.*,table2.*,..');

像这样,另一件事是如果 table1 和 table2 的任何列相同,那么你需要选择 alias

$this->db->select('table1.id as tab1id,table2.id as tab2id');

上表1,2是相同的列名“id”

【讨论】:

  • 查询在我的 qustion 第一部分我已加入拖车表并且工作正常,但如果我添加另一个加入然后注意 get 的显示,第三个则无法正常工作
【解决方案2】:

试试这个我认为它的工作

$this->db->select('table1.*,table2.t2_name,table3.t3_name,table4.t4_name');
$this->db->from('table1','table2','table3','table4');

$this->db->join('table2', 'table1.t2_id= table2.t2_id');
$this->db->join('table3', 'table1.t3_id= table3.t3_id');
$this->db->join('table4', 'table1.t4_id= table4.t4_id');

return $this->db->get()->result();

或者您可以使用 SQL 查询来获取结果....

$this->db->query($sql)->result();

【讨论】:

  • 不工作(只是我想在我的第一个表与其他 3 个@RS Sisodia 有关系时将 1 个表连接到 3 个表
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-04
  • 2014-04-24
  • 1970-01-01
相关资源
最近更新 更多