如何检查 mysql 搜索 JOIN 查询期间是否存在行?

发布于 2024-12-28 02:49:52 字数 992 浏览 4 评论 0原文

我有这个有效的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

难以启齿的温柔 2025-01-04 02:49:52

假设即使投票表中不存在投票,您也希望得到结果,< code>LEFT JOIN 投票表。

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
        LEFT 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")

注意:我已经修改了您的初始查询(不是第二个),因为您说它仍然没有提供您想要的内容。

Assuming that you want results even when no votes exist in the votes tables, LEFT JOIN 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
        LEFT 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")

Note: I've modified your initial query (not the second) as you said it still didn't provide what you wanted.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文