Let's say, there are two tables:
select * from users;
+-------+------+
| login | type |
+-------+------+
| test1 | A |
| test2 | A |
| test3 | B |
+-------+------+
and
select * from money;
+-------+--------+------------+
| login | amount | date |
+-------+--------+------------+
| test1 | 10.00 | 1325379661 |
| test1 | 20.00 | 1357002061 |
| test2 | 33.30 | 1388538061 |
+-------+--------+------------+
I need to select login&type fields from the users table and SUM of the amount field from the second table within the selected date range. If sum is empty for the selected period or there are no any records for a login in the 'money' table, then return zero value (not empty set).
I'm doing the following way:
SELECT u.login,u.type, SUM(m.amount) as amount
FROM users AS u
LEFT JOIN money AS m ON m.login=u.login
WHERE date>1388710861 GROUP BY u.login;
This query reasonably returns empty set but I need to get the list of users and zero amount for them:
+-------+------+--------+
| login | type | amount |
+-------+------+--------+
| test1 | A | 0.00 |
| test2 | A | 0.00 |
| test3 | B | 0.00 |
+-------+------+--------+
How is that possible?