MySQL 按 2 列排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/944134/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 13:24:45  来源:igfitidea点击:

MySQL Sort By 2 Columns

mysqlsql-order-by

提问by Ben Shelock

I have a table which holds information on television programs and I want to order it by Seasons and then by episodes. Here's a basic view of what I have:

我有一张表格,其中包含有关电视节目的信息,我想按季节和剧集对其进行排序。这是我所拥有的基本视图:

+---+--------+---------+
|id | Season | Episode |
+---+--------+---------+
| 1 |    1   |    1    |
+---+--------+---------+
| 1 |    1   |    2    |
+---+--------+---------+
| 1 |    2   |    1    |
+---+--------+---------+
| 1 |    2   |    3    |
+---+--------+---------+

So I select what I need and order by Season. But there's going to be a lot between seasons so I need to sort episodes too, but without it affecting seasons.

所以我选择我需要的东西并按季节订购。但是季节之间会有很多,所以我也需要对剧集进行排序,但不会影响季节。

回答by Stefan Gehrig

Do you mean:

你的意思是:

SELECT id, Season, Episode 
FROM table 
ORDER BY Season ASC, Epsisode ASC

Sorting by multiple columns is as simple as it gets.

按多列排序非常简单。

回答by SO User

We know what you mean :) In your order by you should have

我们知道您的意思:) 在您的订单中,您应该有

ORDER BY Season, Episode 

It will sort by Season and then on Episode within Season

它将按季节排序,然后按季节内的情节排序