在 postgresql 中对每列使用不同的标准进行排序,以防出现平局
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16004966/
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
Sorting in postgresql with different criteria to each column in case of tie
提问by user2233540
I have a problem with sorting in postgresql. The information must be sorted in descending order according to the first column, but in case of a tie, the information must be sorted in ascending order according to the second column.
我在 postgresql 中排序有问题。信息必须按照第一列降序排列,但如果出现平局,则信息必须按照第二列升序排列。
How can I accomplish this ?
我怎样才能做到这一点?
回答by Dondi Michael Stroma
SELECT ... ORDER BY col1 DESC, col2 ASC
SELECT ... ORDER BY col1 DESC, col2 ASC
回答by Clodoaldo Neto
select *
from t
order by c1 desc, c2 asc
回答by JayC
Of course.
当然。
http://www.postgresql.org/docs/9.2/static/sql-select.html#SQL-ORDERBY
http://www.postgresql.org/docs/9.2/static/sql-select.html#SQL-ORDERBY
The ORDER BY clause causes the result rows to be sorted according to the specified expression(s). If two rows are equal according to the leftmost expression, they are compared according to the next expression and so on.
ORDER BY 子句使结果行根据指定的表达式进行排序。如果根据最左边的表达式两行相等,则根据下一个表达式进行比较,以此类推。
and later
然后
Optionally one can add the key word ASC (ascending) or DESC (descending) after any expression in the ORDER BY clause.
可以选择在 ORDER BY 子句中的任何表达式后添加关键字 ASC(升序)或 DESC(降序)。
one more:
多一个:
Note that ordering options apply only to the expression they follow; for example ORDER BY x, y DESC does not mean the same thing as ORDER BY x DESC, y DESC.
请注意,排序选项仅适用于它们所遵循的表达式;例如 ORDER BY x, y DESC 与 ORDER BY x DESC, y DESC 的含义不同。
Emphasis, my own.
重点,我自己。