gpt4 book ai didi

mysql - SQL 按月 DESC 和年 ASC 排序

转载 作者:行者123 更新时间:2023-11-29 06:26:42 25 4
gpt4 key购买 nike

我有这个 SQL 查询:

SELECT * FROM `billing` 
where source = 'VOIP'
group by month(timestamp), year(timestamp)
order by month(timestamp) desc, year(timestamp) asc

上面的查询返回这个顺序:

2014-12-01
2014-11-01
2014-10-01
2015-05-01
2015-04-01
2015-03-01
2015-02-01
2015-01-01

我想这样订购:

2015-05-01
2015-04-01
2015-03-01
2015-02-01
2015-01-01
2014-12-01
2014-11-01
2014-10-01

最佳答案

按日期降序排列如何?

SELECT *
FROM `billing`
where source = 'VOIP'
group by month(timestamp), year(timestamp)
order by MIN(timestamp) desc;

注意:您应该永远不要SELECT *GROUP BY 一起使用。您应该明确选择所需的列以及聚合函数。像这样的东西:

SELECT month(timestamp), year(timestamp), count(*)
FROM `billing`
where source = 'VOIP'
group by month(timestamp), year(timestamp)
order by MIN(timestamp) desc;

(获取每个月的记录数。)

关于mysql - SQL 按月 DESC 和年 ASC 排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30560453/

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