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

1 回答 1

1

假设即使投票表中不存在投票,您也想要结果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")

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

于 2012-01-18T20:31:14.323 回答