gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 04:19:44 24 4
gpt4 key购买 nike

我有以下“统计”表:

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

最佳答案

为什么要这么复杂的查询?你可以简单地做

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com