MYSQL按月,年分组获得月份最高分

时间:2015-06-13 08:32:47

标签: mysql

请有人帮帮我吗?

我正在努力让最后10名获奖者参加本月比赛的最新照片,不包括当月,这是我目前的情况: -

SELECT
            *,
            MAX(total_score)
        FROM
            comp_images
        WHERE
            date_added BETWEEN DATE_SUB(curdate(),Interval 10 Month) AND curdate()
        AND
            CONCAT(MONTH(curdate()),'',YEAR(curdate())) != CONCAT(MONTH(date_added),'',YEAR(date_added))
        AND
            url != ''
        AND
            hide = '0'
        GROUP BY
            MONTH(date_added)
        ORDER BY
            total_score DESC
        LIMIT 10

非常感谢任何帮助。

由于

2 个答案:

答案 0 :(得分:0)

我的猜测是你得到了所有图像,最高总分。这里的问题是你是单独选择所有行(带*)和MAX(total_score)但是没有将它们绑定在一起。

你可以做的就是使用MySQL的HAVING关键字。您可以为select all rows语句指定条件。例如:

SELECT * FROM comp_images..... HAVING MAX(total_score);

答案 1 :(得分:0)

以防其他人在此处遇到同样的问题是我找到的解决方案:

SELECT
            * , CONCAT(MONTHNAME(date_added),' ',YEAR(date_added)) as win_month
        FROM (select * from comp_images order by total_score desc) x
        WHERE
            date_added BETWEEN DATE_SUB(curdate(),Interval 10 Month) AND curdate()
        AND
            CONCAT(MONTH(curdate()),'',YEAR(curdate())) != CONCAT(MONTH(date_added),'',YEAR(date_added))
        AND
            url != ''
        AND
            hide = '0'
        GROUP BY
            MONTH(date_added)
        LIMIT 10