MySQL Orderby a number, Nulls last

前端 未结 12 2619
再見小時候
再見小時候 2020-11-22 16:57

Currently I am doing a very basic OrderBy in my statement.

SELECT * FROM tablename WHERE visible=1 ORDER BY position ASC, id DESC

The probl

12条回答
  •  一个人的身影
    2020-11-22 17:33

    MySQL has an undocumented syntax to sort nulls last. Place a minus sign (-) before the column name and switch the ASC to DESC:

    SELECT * FROM tablename WHERE visible=1 ORDER BY -position DESC, id DESC
    

    It is essentially the inverse of position DESC placing the NULL values last but otherwise the same as position ASC.

    A good reference is here http://troels.arvin.dk/db/rdbms#select-order_by

提交回复
热议问题