1

I have a GROUP_CONCAT using a sub-query however the sub-query is throwing an error 'unknown column m.itemid...'.

I think this maybe a scope issue but i am having major problems finding a solution with my limited knowledge of MySQL. ANy help is appreciated and if you need more information please let me know.

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 Answer 1

1

I guess you can use joins in your outer query this way you don't need to do the correlated subquery

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.