Is there any way to use the sum yield in group_concat or any work around?
I have the following query, which returns redundant results for for item_id and items.title fields:
SELECT
item_id, sum(qty), items.title, units.title
FROM invoice_items
LEFT JOIN items
ON item_id = items.id
LEFT JOIN units ON invoice_items.unit_id = units.id
GROUP BY item_id, invoice_items.unit_id ORDER BY items.id, units.weight DESC;
I tried to use group_concat to concat redundant rows as follows:
SELECT
sum(qty), group_concat(item_id, items.title, sum(qty) SEPARATOR '-')
FROM invoice_items
LEFT JOIN items
ON item_id = items.id
LEFT JOIN units ON invoice_items.unit_id = units.id
GROUP BY item_id, invoice_items.unit_id ORDER BY items.id, units.weight DESC;
However, it returns this error: Invalid use of group function
What I want is to concatenate the sum yield in one row regard less of any other fields.
The following is a screen shot for the query result:
I need first row to be something like:
item_id: 1
sum(qty): 2, 13, 5
title: اسبوسيد أقراص

invoice_items.unit_idfromgroup bywill fix the problem. Why have you usedinvoice_items.unit_idingroup bysum()result is only available after all the relevant rows/groups have been discovered, which means it's NOT available at the time mysql is starting to concatenate the rows it's finding.**SUMGOESHERE**and then do a string replacement on the eventual concatted result, and substitute in your sum() value.