-2

这个节目最后 10 个新结果,一切正常

$query = "SELECT s.name AS song,
                 SUM(v.hit_shit) AS hit,
                 COUNT(v.hit_shit) - SUM(v.hit_shit) AS shit,
                 MAX(date_voted) AS last_vote
            FROM songs AS s
           INNER JOIN votes AS v ON v.song_id = s.id
           WHERE v.date_voted >= CURDATE()
           GROUP BY s.id
           ORDER BY last_vote DESC
           LIMIT 10";
$song = mysql_query($query);

但这说明了一切,但我只需要 5 个 :(

$query = "SELECT s.name AS song,
                 SUM(v.hit_shit) AS hit,
                 COUNT(v.hit_shit) - SUM(v.hit_shit) AS shit
            FROM songs AS s
           INNER JOIN votes AS v ON v.song_id = s.id
           WHERE v.date_voted >= CURDATE()
             AND v.date_voted < CURDATE() + INTERVAL 1 DAY
           GROUP BY s.id
           ORDER BY hit DESC, shit ASC, song ASC";
$song = mysql_query($query);

ps对不起4我的英语不好

4

2 回答 2

1

将限制 5 添加到查询的末尾。

$query = "SELECT s.name AS song,
                 SUM(v.hit_shit) AS hit,
                 COUNT(v.hit_shit) - SUM(v.hit_shit) AS shit,
                 MAX(date_voted) AS last_vote
            FROM songs AS s
           INNER JOIN votes AS v ON v.song_id = s.id
           WHERE v.date_voted >= CURDATE()
           GROUP BY s.id
           ORDER BY last_vote DESC
           LIMIT 5";

$song = mysql_query($query);
于 2013-10-23T16:31:36.380 回答
0

您应该在查询末尾添加参数“LIMIT”。

$query = "SELECT s.name AS song,
                 SUM(v.hit_shit) AS hit,
                 COUNT(v.hit_shit) - SUM(v.hit_shit) AS shit
            FROM songs AS s
           INNER JOIN votes AS v ON v.song_id = s.id
           WHERE v.date_voted >= CURDATE()
             AND v.date_voted < CURDATE() + INTERVAL 1 DAY
           GROUP BY s.id
           ORDER BY hit DESC, shit ASC, song ASC
           LIMIT 5";

$song = mysql_query($query);

编辑:修改代码格式。

于 2013-10-23T16:33:28.273 回答