mysql按升序获取最新的日期记录

时间:2015-02-04 14:10:40

标签: php mysql

我有以下“统计数据”表:

enter image description here

我希望mysql查询以点击总和返回最新的5个日期。所以开始记录应该是2015-01-12

输出:

enter image description here

到目前为止,我已根据我的输出编写了以下查询。

SELECT logdate, (
    SELECT SUM(clicks)
    FROM stats t2
    WHERE t2.logdate = t1.logdate
    ORDER BY t2.logdate
    DESC LIMIT 0,5
) as clicks
FROM stats t1
GROUP BY t1.logdate
ORDER BY t1.logdate ASC
LIMIT 0, 5

0 个答案:

没有答案