MySQL 如何在 SELECT 查询中按多列排序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2099923/
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
How do I order by multiple columns in a SELECT query?
提问by tibin mathew
I have a table of records like below
我有一个像下面这样的记录表
int_record_id int_category_id str_name int_order bit_active 1 1 test1 2 1 2 1 test2 1 1 3 2 test3 1 1 1 3 test4 3 1
i want to select this record in a such a way that it should be sorted in the order of both int_category_id
and int_order
我想在这样的方式来选择这个记录,它应该在两者的顺序进行排序int_category_id
,并int_order
so the result should be like below
所以结果应该如下所示
int_record_id int_category_id str_name int_order bit_active 2 1 test2 1 1 1 1 test1 2 1 3 2 test3 1 1 4 3 test4 3 1
Does any one have an idea about its sql query, i have tried a lot i'm not getting the result correct. can any one show me the exact sql query for this.
有没有人知道它的 sql 查询,我尝试了很多我都没有得到正确的结果。任何人都可以向我展示确切的 sql 查询。
回答by Klaus Byskov Pedersen
select * from your_table order by int_category_id, int_order
回答by Oded
This query should do:
这个查询应该做:
select * from myTable
order by int_category_id, int_order
You need to decide what you primary sort will be and inside that the secondary (and so on).
你需要决定你的主要排序是什么,以及在次要排序中(等等)。
So, if you want to sort first by order then by category, you would use:
所以,如果你想先按顺序再按类别排序,你可以使用:
select * from myTable
order by int_order, int_category_id
See the mySql order bydocumentation.
请参阅文档中的mySql 顺序。