SQL 多级排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5742274/
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
Multiple level ordering
提问by Sapan
I have a table with some records with fields like name, rating etc.
我有一个表,其中包含一些记录,其中包含名称、评级等字段。
I first want to sort based on rating limiting results to 20 and then on this resultset want to further apply sort based on name.
我首先想根据将结果限制为 20 的评级进行排序,然后在此结果集上希望进一步应用基于名称的排序。
I know to sort we need to use the query like
我知道排序我们需要使用这样的查询
Select * from table order by rating Desc limit 20
but on this resultset how to apply another level of ordering? How can I combine these two sorts in one sqlite statement?
但是在这个结果集上如何应用另一个级别的排序?如何将这两种类型组合在一个 sqlite 语句中?
回答by ThiefMaster
You could use e.g. ORDER BY rating DESC, name ASC
to sort by rating and then, if the ratings are equal, by name.
您可以使用例如ORDER BY rating DESC, name ASC
按评级排序,然后,如果评级相等,则按名称排序。
回答by JuanBoca
This query should do the trick:
这个查询应该可以解决问题:
SELECT * FROM (SELECT * FROM table ORDER BY rating DESC LIMIT 20) ORDER BY name