To calculate the price of invoice
s (that have *invoice item
*s in a separate table and linked to the invoice
s), I had written this query:
SELECT `i`.`id`, SUM(ii.unit_price * ii.quantity) invoice_price
FROM (`invoice` i)
JOIN `invoiceitem` ii
ON `ii`.`invoice_id` = `i`.`id`
WHERE `i`.`user_id` = '$user_id'
But it only resulted ONE row.
After research, I got that I had to have GROUP BY i.id
at the end of the query. With this, the results were as expected.
From my opinion, even without GROUP BY i.id
, nothing is lost and it should work well!
Please in some simple sentences tell me...
Why should I always use the additional!!! GROUP BY i.id
, What is lost without it, and maybe as the most functioning question, How should I remember that I have lost the additional GROUP BY
?!