2014-05-15 352 views
1

我有一個GROUP_CONCAT使用子查詢,但是子查詢拋出一個錯誤'unknown column m.itemid ...'。MySQL GROUP_CONCAT使用子查詢

我認爲這可能是一個範圍問題,但我有重大問題找到解決方案與我有限的MySQL知識。任何幫助表示讚賞,如果您需要更多信息,請讓我知道。

SELECT *, 
     CASE parent_item_id 
     WHEN 0 THEN itemid 
     ELSE parent_item_id 
     end AS order_items_sort, 
     (SELECT Group_concat(id) 
     FROM (SELECT i.install_delivery_id `id` 
       FROM machine_serials s 
         INNER JOIN install_delivery_items i 
           ON s.install_delivery_item_id = i.id 
       WHERE s.machine_id = m.itemid 
       GROUP BY i.install_delivery_id) g) `delivery_id` 
FROM machines m 
WHERE m.orderid = 36549 
ORDER BY order_items_sort, parent_item_id ASC 

回答

1

我想你可以用你的外部查詢聯接這種方式,你不需要做相關子查詢

SELECT m.*, 
(CASE parent_item_id 
    WHEN 0 THEN itemid 
    ELSE parent_item_id 
END) AS order_items_sort, 
GROUP_CONCAT(DISTINCT i.install_delivery_id) `delivery_id` 
FROM machines m 
LEFT JOIN machine_serials s ON(s.machine_id = m.itemid) 
LEFT JOIN install_delivery_items i ON s.install_delivery_item_id = i.id 
WHERE m.orderid = 36549 
GROUP BY m.itemid 
ORDER BY order_items_sort, m.parent_item_id ASC 
+1

成因。謝謝。完美的作品。 –