我有以下“统计”表:
我希望mysql查询返回最新的5个日期和点击的总和。所以开始记录应该是2015-01-12
输出:
到目前为止,我已经编写了以下查询,但根据我的输出,这些查询不起作用。

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

最佳答案

为什么这么复杂的问题?你可以简单地

SELECT logdate,sum(clicks) FROM stats
GROUP BY logdate
ORDER BY logdate DESC
LIMIT 5

在评论后编辑
SELECT * FROM
(
 SELECT logdate,sum(clicks) FROM stats
 GROUP BY logdate
 ORDER BY logdate DESC
 LIMIT 5
) data
 ORDER BY logdate ASC

Fiddle

关于php - mysql按升序获取最新日期记录,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28323466/

10-16 14:36