How to check if rows exist during a mysql search JOIN query?
Question
I have this sql search query that works, but it only works if votes exist in the votes table.
mysql_query("SELECT m.Title, r.Subject, SUM(v.IsGood) AS Good, SUM(v.IsBad) AS Bad
FROM Movies m
JOIN Reviews r
ON m.ID=r.MovieID
JOIN Votes v
ON r.ID=v.ReviewID
WHERE (m.Title LIKE '%" . $search . "%' OR r.Subject LIKE '%" . $search . "%')
GROUP BY m.Title, r.Subject
ORDER BY SUM(v.IsGood) DESC, SUM(v.IsBad) ASC LIMIT 10")
So I've changed it to this below so that no matter if there is 0 votes it should still show. But it doesn't pull any results, can anyone tell me what am I doing wrong?
mysql_query("SELECT m.Title, r.Subject, COUNT(v.ReviewID) AS Rvotes, SUM(v.IsGood) AS Good, SUM(v.IsBad) AS Bad
FROM Movies m
JOIN Reviews r
ON m.ID=r.MovieID
JOIN Votes v
ON r.ID=v.ReviewID
WHERE (m.Title LIKE '%" . $search . "%' OR r.Subject LIKE '%" . $search . "%')
GROUP BY m.Title, r.Subject, v.ReviewID
HAVING Rvotes >= 0
ORDER BY SUM(v.IsGood) DESC, SUM(v.IsBad) ASC LIMIT 10")
No correct solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow