按两列排序 MySQL 表

Order a MySQL table by two columns(按两列排序 MySQL 表)
本文介绍了按两列排序 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 表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)
SQL/MySQL: split a quantity value into multiple rows by date(SQL/MySQL:按日期将数量值拆分为多行)