mysql order by,先为空,后为 DESC

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9307613/
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 12:09:43  来源:igfitidea点击:

mysql order by, null first, and DESC after

mysqlsql-order-by

提问by Ervin

How can I order DESC by a field, but list the NULL values first?

如何按字段对 DESC 进行排序,但首先列出 NULL 值?

So I'm having a table:

所以我有一张桌子:

reuestId | offerId | offerTitle
1        | 1       | Alfa
NULL     | 2       | Beta
2        | 3       | Gamma

I want to select them so that the results would be:

我想选择它们,以便结果是:

NULL | 2 | Beta
2    | 3 | Gamma
1    | 1 | Alfa

回答by DonCallisto

Try this:

尝试这个:

ORDER BY [reuestId] IS NULL DESC, [reuestId] DESC

should work (for mySql)

应该工作(对于mySql)

回答by ypercube??

SELECT *
FROM TableX
ORDER BY (requestId IS NOT NULL)
       , requestId DESC