2013-02-13 132 views
0

我有一個查詢,看起來像這樣:MySQL的GROUP_CONCAT在加入

SELECT 
    *, 
    GROUP_CONCAT(DISTINCT web_order_item.od_pcode) AS items, 
    GROUP_CONCAT(DISTINCT woi_id) AS woi_ids 
    FROM web_order 
JOIN web_order_item USING(od_id) 
JOIN customer_master ON customer_master.VC_CUSTOMER_CODE = web_order.p_vendor 
LEFT JOIN production_item ON production_item.ItemID IN (GROUP_CONCAT(DISTINCT woi_id)) AND QCStatus <> 'received' 
WHERE 
    web_order.od_date >= FROM_UNIXTIME(1357257600) 
    AND web_order.od_date <= FROM_UNIXTIME(1359936000) 
    AND web_order.od_id > 0 
    AND 1 
GROUP BY web_order.od_id 
ORDER BY web_order.od_id DESC 

此查詢引發錯誤

General error: 1111 Invalid use of group function

缺少什麼我在這裏?謝謝

回答

0

您正在選擇原始記錄的全部內容,然後在web_order_item上執行組concat,由於DISTINCT屬性,這將生成單行,如果有多條記錄,那麼您在那裏分組,然後嘗試再次分組... ...無論如何,你最後還是在分組?沒有聚合,訂單就足夠了。取出組由...或更改組通過對包括組CONCAT之前的所有相關項目...

0
SELECT 
*, 
GROUP_CONCAT(DISTINCT web_order_item.od_pcode) AS items, 
GROUP_CONCAT(DISTINCT woi_id) AS woi_ids 
FROM web_order 
JOIN web_order_item USING(od_id) 
JOIN customer_master ON customer_master.VC_CUSTOMER_CODE = web_order.p_vendor 
LEFT JOIN production_item ON production_item.ItemID IN (GROUP_CONCAT(DISTINCT woi_id))  AND QCStatus <> 'received' 
WHERE 
web_order.od_date >= FROM_UNIXTIME(1357257600) 
AND web_order.od_date <= FROM_UNIXTIME(1359936000) 
AND web_order.od_id > 0 
AND 1 
GROUP BY items, woi_ids 
ORDER BY web_order.od_id DESC 

跳這有助於

參考http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html