mysql - SQL 按月 DESC 和年 ASC 排序

标签 mysql sql

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

相关文章:

mysql - 如何简化这个sql查询

mysql - 如何在 VBA 代码中获取 mysql 存储函数结果

SQL 服务器 : update boolean column based on conditions from 2 other columns

javascript - SQL 日期时间值格式

php - 如何摆脱 json_encode() : Invalid UTF-8 sequence in php?

mysql - Rails动态添加条件到mysql查询

mysql - 构建 SQL 连接以补充数据。需要 T-SQL 帮助

mysql - 使用游标的过程循环中出错

mysql - 将 mySQL 查询转换为 Zend FrameWork 查询的工具

php - 订购带有小数和非小数的 varchar 字段