I've been looking around for alot of people having the same problem, but the solution seems te be much more different?!
I have a 3 tables:
+-------------------------------------------+
| ITEM: itemid(pk), status(fk), owner(fk) |
| |
| STATUS: statusid(pk), statusname |
| |
| OWNER: ownerid(pk), ownername |
+-------------------------------------------+
I have 2 status: 1: Available, 2: Broken.
In this case there is only ONE owner with ONE broken item. I am using this code. First it was different, but it seems LEFT JOIN and GROUP/ORDER BY should do the trick. Unfortunately for me.
SELECT ownername, SUM(price) AS 'totalbrokenpriceeach', COUNT( itemid ) AS 'totalbrokenitemseach'
FROM item
LEFT JOIN owner ON item.owner = owner.ownerid
LEFT JOIN status on item.status = status.statusid
WHERE statusid='2'
GROUP BY ownerid
This returns:
ownername totalbrokenpriceeach totalbrokenitemseach
Owner #1 60 1
Owner #5 180 4
I'd like to have returned:
ownername totalbrokenpriceeach totalbrokenitemseach
Owner #1 60 1
Owner #2 0 0
Owner #3 0 0
Owner #4 0 0
Owner #5 180 4
What to do? Anyone any solutions?
EDIT:
OWNER
+-----------------------+
| ownerid ownername |
| 1: Henk |
| 2: Jan |
| 3: Piet |
| 4: Klaas |
+-----------------------+
STATUS
+-----------------------+
| statusid statusbeschr |
| 1: Available |
| 2: Broken |
+-----------------------+
ITEM
+--------------------------------------+
| itemid price owner status |
| 1: 90 1 1 |
| 2: 40 2 1 |
| 3: 20 2 1 |
| 4: 120 3 2 |
+--------------------------------------+
I need returned:
+-------------------------------------------------------+
| ownername SumOfBrokenItems AmountOfBrokenItems |
| Henk: 0 0 |
| Jan: 0 0 |
| Piet: 120 1 |
| Klaas: 0 0 |
+-------------------------------------------------------+