CodeIgniter Subquery in Query Builder

拜拜、爱过 提交于 2021-01-29 06:19:43

问题


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://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.


回答1:


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


来源:https://stackoverflow.com/questions/51960117/codeigniter-subquery-in-query-builder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!