Query rows using Order by A, if A values are the same, Order by B as the second standard

删除回忆录丶 提交于 2021-01-28 14:52:45

问题


I want to query db rows using two standards: A first, B second. That is: Order by A, if A values are the same, Order by B as the second standard How to write the sql? Example: query table:

id | A | B
_ _ _ _ _ _
1  | 1 | 1
_ _ _ _ _ _
2  | 2 | 2 
_ _ _ _ _ _
3  | 2 | 1
_ _ _ _ _ _
4  | 3 | 1

query result:

id
1
3
2
4

回答1:


Order by is used to sort the result from a table in ASC | DESC based on one or more column names. It sorts by ASC in default.

Example:

Select * from Table1 order by A, B

In this example the results from Table1 is sorted in ASC by A as well as B. If A has the same values, then the results will be sorted by B in ASC




回答2:


You can simply have multiple order-by's: ORDER BY A DESC,B for example.




回答3:


To get the desired result:

Select * from SomeTable ORDER BY A ASC, B ASC


来源:https://stackoverflow.com/questions/32857088/query-rows-using-order-by-a-if-a-values-are-the-same-order-by-b-as-the-second

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!