简体   繁体   English

查询生成器中的CodeIgniter子查询

[英]CodeIgniter Subquery in Query Builder

Is there a way or can someone translate this to code igniter query builder. 有没有办法或有人可以将其转换为代码点火器查询生成器。

SELECT result.id, result.name, result.added
FROM (SELECT tbl.id, tbl.name, tbl.date_added
      FROM table tbl
      GROUP BY tbl.id) result
GROUP BY result.date_added

I already have my research(link below) but can't find anything like this(query above). 我已经进行了研究(下面的链接),但找不到类似的内容(上面的查询)。 https://www.codeigniter.com/userguide3/database/query_builder.html https://www.codeigniter.com/userguide3/database/query_builder.html

https://arjunphp.com/how-to-write-subqueries-in-codeigniter-active-record/ https://arjunphp.com/how-to-write-subqueries-in-codeigniter-active-record/

And yes, this can be done using Stored Procedure but I have another reason why I need to implement this as query builder. 是的,这可以使用存储过程来完成,但是我还有另一个原因需要将其作为查询生成器来实现。

try this one. 试试这个。

// Sub Query
         $this->db->select('result.id, result.name, result.added')->from('table tbl');
     $subQuery =  $this->db->get_compiled_select();

// Main Query
     $this->db->select('result.id, result.name, result.added')
     ->from('table tbl')
     ->where("id IN ($subQuery)", NULL, FALSE)
     ->get()
     ->result();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM