Id Item
--------------
1 ItemA
2 ItemB
3 ItemC
itemid Price
----------------
1 4
1 3
1 9
2 2
2 4
2 3
How I can select with sum from 2 tables? Like as:
ItemA 16
ItemB 9
ItemC 0
You can
JOINthe table using aLEFT JOINand apply an aggregate functionSUM()to thepricefield:See SQL Fiddle with Demo
The
LEFT JOINwill allow for the records not in the second table to be included in the final result. I added theIsNull()to thesum()to replace anynullvalues with a zero.Result: