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 DESC
How 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 表


基础教程推荐
- 在 MySQL 中:如何将表名作为存储过程和/或函数参数传递? 2021-01-01
- 在多列上分布任意行 2021-01-01
- 什么是 orradiag_<user>文件夹? 2022-01-01
- 二进制文件到 SQL 数据库 Apache Camel 2021-01-01
- MySQL 中的类型:BigInt(20) 与 Int(20) 2021-01-01
- 如何根据该 XML 中的值更新 SQL 中的 XML 2021-01-01
- mysql选择动态行值作为列名,另一列作为值 2021-01-01
- 表 './mysql/proc' 被标记为崩溃,应该修复 2022-01-01
- 如何在 SQL 中将 Float 转换为 Varchar 2021-01-01
- oracle区分大小写的原因? 2021-01-01