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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 15:30:24  来源:igfitidea点击:

MYSQL order by both Ascending and Descending sorting

mysqlsqlsorting

提问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 BYOptimization

看看ORDER BY优化

回答by Mahmoud Gamal

I don't understand what the meaning of ordering with the same column ASCand DESCin the same ORDER BY, but this how you can do it: naam DESC, naam ASClike so:

我不明白的排序与同一列什么意思ASC,并DESC在同一个ORDER BY,但是这如何能做到这一点:naam DESC, naam ASC像这样:

ORDER BY `product_category_id` DESC,`naam` DESC, `naam` ASC