mysql order by,null first,和DESC之后

时间:2012-02-16 08:08:17

标签: mysql sql-order-by

如何通过字段订购DESC,但首先列出NULL值?

所以我有一张桌子:

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

我想选择它们,结果如下:

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

2 个答案:

答案 0 :(得分:141)

试试这个:

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

应该有效(对于mySql)

答案 1 :(得分:26)

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