MySQL JOIN查询在不同的数据库

时间:2017-06-08 15:12:54

标签: php mysql

我在我的计算机上本地安装的数据库上运行查询,并在我的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

当在本地运行时,我会从用户表中取回每个条目,即使他们没有在预订表上预订。

当我在我的服务器数据库上运行时,我只回复已经预订的用户,而不是在进行正确加入时进行预期的输入。

1 个答案:

答案 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 JOINprojects表之间users bookings有关。