简体   繁体   English

从Codeigniter Active Record中的子查询中选择

[英]SELECT from subquery in Codeigniter Active Record

How would I do the following query in Codeigniter ActiveRecord : - 如何在Codeigniter ActiveRecord中执行以下查询: -

SELECT *, 

(SELECT 
        image_path
    FROM
        image
    WHERE
        image_table = 'model'
            AND image_table_id = model_id
    GROUP BY image_table_id
    LIMIT 1) AS ModelImg 

FROM

   (SELECT 
      *
   FROM
      vw_newcars
    where offer_table = 'derivative'
    order by offer_order
   ) x

WHERE make_name = 'Fiat'
group by offer_table_id
limit 12

The part I'm having problems with is how to do a select from subquery in Active Record. 我遇到问题的部分是如何从Active Record中的子查询中进行选择。

I don't see a from_select function or anything comparable in the documentation. 我没有在文档中看到from_select函数或类似的东西。

I managed to get the query to work by putting the from sub_query into the initial select statement : 我设法查询通过将工作from sub_query到初始SELECT语句:

$this->db->select("*, 
  (select image_path from image where image_table = 'model' and image_table_id = model_id
  group by image_table_id limit 1) as ModelImg FROM 
  (SELECT * FROM $view where offer_table = 'derivative' order by offer_order) x"); 

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

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