mysql order by, null first, and DESC after

依然范特西╮ 提交于 2019-12-17 17:32:23

问题


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

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

回答1:


Try this:

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

should work (for mySql)




回答2:


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


来源:https://stackoverflow.com/questions/9307613/mysql-order-by-null-first-and-desc-after

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!