SQL 多列排序

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

SQL multiple column ordering

sqlsql-order-by

提问by Se?or Reginold Francis

I am trying to sort by multiple columns in SQL, and in different directions. column1would be sorted descending, and column2ascending.

我正在尝试按 SQL 中的多个列和不同的方向进行排序。column1将按降序和column2升序排序。

How can I do this?

我怎样才能做到这一点?

回答by Ignacio Vazquez-Abrams

ORDER BY column1 DESC, column2

This sorts everything by column1(descending) first, and then by column2(ascending, which is the default) whenever the column1fields for two or more rows are equal.

当两行或更多行的字段相等时,这column1首先按(降序)对所有内容进行排序,然后按column2(升序,这是默认值)排序column1

回答by Thomas C. G. de Vilhena

The other answers lack a concrete example, so here it goes:

其他答案缺乏具体的例子,所以在这里:

Given the following Peopletable:

鉴于以下人员表:

 FirstName |  LastName   |  YearOfBirth
----------------------------------------
  Thomas   | Alva Edison |   1847
  Benjamin | Franklin    |   1706
  Thomas   | More        |   1478
  Thomas   | Jefferson   |   1826

If you execute the query below:

如果您执行以下查询:

SELECT * FROM People ORDER BY FirstName DESC, YearOfBirth ASC

The result set will look like this:

结果集将如下所示:

 FirstName |  LastName   |  YearOfBirth
----------------------------------------
  Thomas   | More        |   1478
  Thomas   | Jefferson   |   1826
  Thomas   | Alva Edison |   1847
  Benjamin | Franklin    |   1706

回答by Quassnoi

SELECT  *
FROM    mytable
ORDER BY
        column1 DESC, column2 ASC

回答by Jason Clark

Multiple column ordering depends on both column's corresponding values: Here is my table example where are two columns named with Alphabets and Numbers and the values in these two columns are ascand descorders.

多列排序取决于两列的对应值:这是我的表格示例,其中两列以字母和数字命名,这两列中的值是ascdesc顺序。

enter image description here

在此处输入图片说明

Now I perform Order Byin these two columns by executing below command:

现在我通过执行以下命令在这两列中执行Order By

enter image description here

在此处输入图片说明

Now again I insert new values in these two columns, where Alphabet value in ASCorder:

现在我再次在这两列中插入新值,其中字母值按ASC顺序:

enter image description here

在此处输入图片说明

and the columns in Example table look like this. Now again perform the same operation:

示例表中的列如下所示。现在再次执行相同的操作:

enter image description here

在此处输入图片说明

You can see the values in the first column are in desc order but second column is not in ASC order.

您可以看到第一列中的值按降序排列,但第二列不是按升序排列。

回答by atik sarker

You can use multiple ordering on multiple condition,

您可以在多个条件下使用多个排序,

ORDER BY 
     (CASE 
        WHEN @AlphabetBy = 2  THEN [Drug Name]
      END) ASC,
    CASE 
        WHEN @TopBy = 1  THEN [Rx Count]
        WHEN @TopBy = 2  THEN [Cost]
        WHEN @TopBy = 3  THEN [Revenue]
    END DESC