Order a MySQL table by two columns(按两列排序 MySQL 表)
问题描述
如何按两列对 MySQL 表进行排序?
我想要的是先按最高评分排序的文章,然后是最近的日期.例如,这将是一个示例输出(左# 是评分,然后是文章标题,然后是文章日期)
<前>+================+================================================+|article_rating |文章 |article_time |+================+================================================+|50 |这篇文章岩石|2009 年 2 月 4 日 |+----------------+------------------------------+---------------+|35 |这篇文章不错|2009 年 2 月 1 日 |+----------------+------------------------------+---------------+|5 |这篇文章不是那么热 |2009 年 1 月 25 日 |+================+================================================+我使用的相关 SQL 是:
ORDER BY article_rating, article_time DESC我可以按其中一个排序,但不能同时排序.
默认排序是升序,需要在两个订单中添加关键字DESC:
ORDER BY article_rating DESC, article_time DESCHow do I sort a MySQL table by two columns?
What I want are articles sorted by highest ratings first, then most recent date. As an example, this would be a sample output (left # is the rating, then the article title, then the article date)
+================+=============================+==============+ | article_rating | article | article_time | +================+=============================+==============+ | 50 | This article rocks | Feb 4, 2009 | +----------------+-----------------------------+--------------+ | 35 | This article is pretty good | Feb 1, 2009 | +----------------+-----------------------------+--------------+ | 5 | This Article isn't so hot | Jan 25, 2009 | +================+=============================+==============+
The relevant SQL I'm using is:
ORDER BY article_rating, article_time DESC
I can sort by one or the other, but not both.
Default sorting is ascending, you need to add the keyword DESC to both your orders:
ORDER BY article_rating DESC, article_time DESC
这篇关于按两列排序 MySQL 表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:按两列排序 MySQL 表
基础教程推荐
- 带更新的 sqlite CTE 2022-01-01
- MySQL 5.7参照时间戳生成日期列 2022-01-01
- 从字符串 TSQL 中获取数字 2021-01-01
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01
- 带有WHERE子句的LAG()函数 2022-01-01
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
