Codeigniter - 加入派生表 [英] Codeigniter - Joining derived tables

查看:125
本文介绍了Codeigniter - 加入派生表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何加入2个基本表。我想知道的是如何通过向 mysql 中添加别名来连接 Codeigniter 中的派生表。

I know how to join 2 basic tables. What I'm interested in knowing is how do I join the derived tables in Codeigniter like we do in mysql by giving aliases to the tables.

例如:

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

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

如何离开加入这两个表格?

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

Please note: I know I can left join first and then apply the where clauses. But that changes the query result. I need to left join the derived tables.

推荐答案

我不知道这是规范的方法,但这是我如何做的例子:

i'm not sure that this is the canonical approach, but this is example how i do it:

// 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();

这篇关于Codeigniter - 加入派生表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆