MySQL Orderby a number, Nulls last

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

    You can swap out instances of NULL with a different value to sort them first (like 0 or -1) or last (a large number or a letter)...

    SELECT field1, IF(field2 IS NULL, 9999, field2) as ordered_field2
      FROM tablename
     WHERE visible = 1
     ORDER BY ordered_field2 ASC, id DESC
    

提交回复
热议问题