从LEFT OUTER JOIN计算NULL值,这可能吗?

时间:2014-05-10 03:54:13

标签: mysql sql join

如果有人可以提供帮助,我有一个与加入相关的问题

我有一个问题:

SELECT cl.*,c.*,s.*,o.*,p.*,r.*
FROM COMPANY_LOCATION__K cl
INNER JOIN COMPANY__K c on c.COMPANY_ID__K = cl.COMPANY_ID__K
INNER JOIN SOCIAL_MEDIA__K s on cl.COMPANY_ID__K = s.COMPANY_ID__K
INNER JOIN OPERATIONAL_HOURS__K o on cl.LOCATION_ID__K = o.LOCATION_ID__K
INNER JOIN PERMISSIONS__K p on cl.LOCATION_ID__K = p.LOCATION_ID__K
LEFT OUTER JOIN REVIEW__K r on cl.LOCATION_ID__K = r.LOCATION_ID__K
ORDER BY DISTANCE__K LIMIT 100;

这将列出有关公司和许多其他统计数据的信息。

我遇到了麻烦,因为a Company_Location可能不一定与评论相关联,所以当我将r.*更改为COUNT(r.rating)时,没有评论的行没有显示在我的结果集中。我希望伯爵以0回来,所以我知道没有评级存在。这有可能吗?

提前致谢

1 个答案:

答案 0 :(得分:0)

我相信你需要一个group by来完成你需要的东西。如果你想要每个公司的信息,我认为那将是:

SELECT cl.*, c.*, s.*, o.*, p.*, count(r.rating)
FROM COMPANY_LOCATION__K cl
INNER JOIN COMPANY__K c on c.COMPANY_ID__K = cl.COMPANY_ID__K
INNER JOIN SOCIAL_MEDIA__K s on cl.COMPANY_ID__K = s.COMPANY_ID__K
INNER JOIN OPERATIONAL_HOURS__K o on cl.LOCATION_ID__K = o.LOCATION_ID__K
INNER JOIN PERMISSIONS__K p on cl.LOCATION_ID__K = p.LOCATION_ID__K
LEFT OUTER JOIN REVIEW__K r on cl.LOCATION_ID__K = r.LOCATION_ID__K
GROUP BY cl.LOCATION_ID
ORDER BY DISTANCE__K
LIMIT 100;