如何检查 mysql 搜索 JOIN 查询期间是否存在行?
我有这个有效的 SQL 搜索查询,但只有当投票表中存在投票时它才有效。
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")
所以我把它改成下面这样,这样无论有没有0票它仍然应该显示。但它没有任何结果,谁能告诉我我做错了什么?
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")
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")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设即使投票表中不存在投票,您也希望得到结果,< code>LEFT JOIN 投票表。
注意:我已经修改了您的初始查询(不是第二个),因为您说它仍然没有提供您想要的内容。
Assuming that you want results even when no votes exist in the votes tables,
LEFT JOIN
the votes table.Note: I've modified your initial query (not the second) as you said it still didn't provide what you wanted.