2013-01-10 174 views
0

我有這個疑問:笨查詢問題

$this->db->select('COUNT(tran_ID) AS count, CASE WHEN MONTH(days)>=4 THEN concat(YEAR(days), "-", YEAR(days)+1) ELSE concat(YEAR(days)-1, "-", YEAR(days)) END AS format_date,product_class AS saleCol'); 

我得到語法錯誤的

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHEN MONTH(days)>=4 THEN concat(YEAR(days), `"-"`, YEAR(days)+1) ELSE concat(YEA' at line 1 

SELECT COUNT(tran_ID) AS count, `CASE` WHEN MONTH(days)>=4 THEN concat(YEAR(days), `"-"`, YEAR(days)+1) ELSE concat(YEAR(days)-1, `"-"`, YEAR(days)) END AS format_date, `product_class` AS saleCol FROM (`transactions`) WHERE `trans_type` = 'E' AND `product_class` != '0' GROUP BY `format_date`, `product_class` 
+1

如果你要運行一個高級查詢,你可以通過'query()'來運行整個事情。活動記錄適用於簡單的方法,但無論如何,query()都可以讓您按照自己的意願進行操作。 –

回答

1

嘗試此查詢,添加FALSE在SELECT語句中的第二個參數

$this->db->select(
        'COUNT(tran_ID) AS count, 
        CASE WHEN MONTH(days)>=4 THEN concat(YEAR(days), "-", YEAR(days)+1) 
        ELSE concat(YEAR(days)-1, "-", YEAR(days)) 
        END AS format_date, 
        product_class AS saleCol',FALSE); 
+0

感謝LearneR secound的參與者完成了這項工作,歡呼! – Dinesh

+0

我曾遇到複雜查詢的相同問題。很高興聽到它解決了您的問題。 –