我有这个 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/