我在我的计算机上本地安装的数据库上运行查询,并在我的AWS RDS数据库上运行时得到不同的结果。 我正在跑步:
SELECT SUM(Hours) AS 'TotalHours', Username, Name, Project_Name, Project_Ref, (37.5-SUM(Hours)) AS 'Missing'
FROM bookings b
JOIN projects p ON b.Project_ID=p.Project_ID
RIGHT JOIN users u ON b.User_ID=u.User_ID
WHERE ((Date BETWEEN '2017-07-01' AND '2017-07-30') OR Date IS NULL)
GROUP BY u.User_ID
当在本地运行时,我会从用户表中取回每个条目,即使他们没有在预订表上预订。
当我在我的服务器数据库上运行时,我只回复已经预订的用户,而不是在进行正确加入时进行预期的输入。
答案 0 :(得分:0)
尝试使用LEFT JOIN
代替RIGHT JOIN
,并将Date
条件放在ON
子句中:
SELECT SUM(Hours) AS 'TotalHours', Username, Name, Project_Name, Project_Ref, (37.5-SUM(Hours)) AS 'Missing'
FROM users AS u
LEFT JOIN bookings AS b ON b.User_ID = u.User_ID AND b.DATE BETWEEN '2017-07-01' AND '2017-07-30'
LEFT JOIN projects AS p ON b.Project_ID=p.Project_ID
我怀疑该问题与INNER JOIN
和projects
表之间users
bookings
有关。