MySQL Orderby a number, Nulls last

前端 未结 12 2690
再見小時候
再見小時候 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:22

    You can coalesce your NULLs in the ORDER BY statement:

    select * from tablename
    where 
    order by
        coalesce(position, 0) ASC, 
        id DESC
    

    If you want the NULLs to sort on the bottom, try coalesce(position, 100000). (Make the second number bigger than all of the other position's in the db.)

提交回复
热议问题