【问题标题】:Codeigniter - Joining derived tablesCodeigniter - 连接派生表
【发布时间】:2016-09-18 12:51:05
【问题描述】:

我知道如何连接 2 个基本表。我有兴趣知道如何通过为表提供别名来加入 Codeigniter 中的派生表,就像我们在 mysql 中所做的那样。

例子:

Table1->where(something)->group_by(something) as derived_table1

Table2->where(something AND something)->group_by(something) as derived_table2

我如何离开加入这 2 个派生表?

请注意: 我知道我可以先离开加入,然后再应用 where 子句。但这会改变查询结果。我需要左加入派生表。

【问题讨论】:

    标签: php mysql codeigniter join


    【解决方案1】:

    我不确定这是规范的方法,但这是我如何做到的示例:

    // Build subquery
    $this->db->select('max(a) a', false);
    $this->db->group_by('b');
    // Save subquery
    $subQuery =  $this->db->get_compiled_select('table-1');
    
    // Main query
    $this->db->select('t.a', false);
    $this->db->from('table-2'); 
    // Here insert subquery
    $this->db->join("($subQuery) t", 'condition', 'left');
    $query = $this->db->get();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-28
      • 2023-03-05
      • 1970-01-01
      • 2016-01-05
      • 1970-01-01
      • 2011-10-30
      • 2020-07-24
      • 1970-01-01
      相关资源
      最近更新 更多