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

时间:2015-04-28 11:44:50

标签: php mysql codeigniter

如何在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

我遇到问题的部分是如何从Active Record中的子查询中进行选择。

我没有在文档中看到from_select函数或类似内容。

1 个答案:

答案 0 :(得分:3)

我设法通过将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");