2015-04-28 59 views
1

我怎麼會做笨的ActiveRecord以下查詢: -從子查詢中選擇笨的Active Record

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 

我有問題的部分是如何做到從子查詢中選擇的活動記錄。

我沒有看到from_select函數或文檔中可比較的任何東西。

+0

什麼版本的笨你用嗎? – Faiz

回答

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