Question

I have a question related to joins if anyone can help

I have a query:

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;

That will list information about a company and many other statistics.

I am running into trouble, because the a Company_Location may not necessarily have Reviews associated with it, so when i change the r.* to COUNT(r.rating), Rows that don't have reviews aren't being displayed in my result set. I want the Count to come back as 0 so i know that no ratings, exist. is this possible?

Thanks in advance

Était-ce utile?

La solution

I believe you need a group by to accomplish what you need. If you want information per company, I think that would be:

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;
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top