问题
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