对 MySQL 中的多个字段进行排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4416880/
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 17:58:31  来源:igfitidea点击:

Sorting multiple fields in MySQL

mysqlsortingfield

提问by True Soft

I have a table with 2 fields DATE and IMPORTANCE. Now I want to sort both these fields in DESCENDING ORDER so that the rows are ordered by IMPORTANCE for EACH DATE. For example, if sorted correct, rows should return like this:

我有一个包含 2 个字段 DATE 和 IMPORTANCE 的表。现在我想在 DESCENDING ORDER 中对这两个字段进行排序,以便按 EACH DATE 的重要性对行进行排序。例如,如果排序正确,行应该像这样返回:

Dec 3, 2010 - 10
Dec 3, 2010 - 10
Dec 3, 2010 - 8
Dec 3, 2010 - 7
Dec 3, 2010 - 3
Dec 3, 2010 - 1

Dec 2, 2010 - 10
Dec 2, 2010 - 9
Dec 2, 2010 - 3

Dec 1, 2010 - 8
Dec 1, 2010 - 5
Dec 1, 2010 - 5
Dec 1, 2010 - 4

Is there an efficientway of accomplishing this with only one query statement?

有没有一种只用一个查询语句来实现这一点的有效方法?

回答by True Soft

SELECT * FROM yourtable
ORDER BY `DATE` DESC, `IMPORTANCE` DESC

回答by Arthur Andersen

You can add as many fields to ORDER BYas you want.

您可以根据需要向ORDER BY添加任意数量的字段。

That'd be something like:

那会是这样的:

SELECT * FROM table ORDER BY `date` DESC, `importance` DESC