SQL multiple column ordering

后端 未结 5 2204
梦谈多话
梦谈多话 2020-11-22 02:40

I am trying to sort by multiple columns in SQL, and in different directions. column1 would be sorted descending, and column2 ascending.

How

5条回答
  •  情话喂你
    2020-11-22 03:23

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

    Given the following People table:

     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
    

提交回复
热议问题