MYSQL 按升序和降序排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13468781/
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
MYSQL order by both Ascending and Descending sorting
提问by Tschallacka
I have a mysql table with products.
我有一个包含产品的 mysql 表。
The products have a category ID and a name.
产品具有类别 ID 和名称。
What I'd like to do is order by category id first descending order and then order by product name ascending order.
我想要做的是按类别 id 首先降序排序,然后按产品名称升序排序。
SELECT * FROM `products` ORDER BY `products`.`product_category_id`,`naam` DESC
What i'd like is
我想要的是
SELECT * FROM `products` ORDER BY `products`.`product_category_id`,`naam` DESC,ASC
but that unfortunately doesn't work.
但不幸的是这不起作用。
Is this even possible in mysql to define the sorting order of the second sorting column?
这甚至可以在mysql中定义第二个排序列的排序顺序吗?
回答by hims056
You can do that in this way:
你可以这样做:
ORDER BY `products`.`product_category_id` DESC ,`naam` ASC
Have a look at ORDER BY
Optimization
回答by Mahmoud Gamal
I don't understand what the meaning of ordering with the same column ASC
and DESC
in the same ORDER BY
, but this how you can do it: naam DESC, naam ASC
like so:
我不明白的排序与同一列什么意思ASC
,并DESC
在同一个ORDER BY
,但是这如何能做到这一点:naam DESC, naam ASC
像这样:
ORDER BY `product_category_id` DESC,`naam` DESC, `naam` ASC