SELECT from subquery in Codeigniter Active Record

怎甘沉沦 提交于 2021-01-27 13:41:38

问题


How would I do the following query in 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.

I don't see a from_select function or anything comparable in the documentation.


回答1:


I managed to get the query to work by putting the from sub_query into the initial select statement :

$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"); 


来源:https://stackoverflow.com/questions/29918549/select-from-subquery-in-codeigniter-active-record

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