Possible to use SQL to sort by date but put null dates at the back of the results set?

前端 未结 4 766
萌比男神i
萌比男神i 2021-01-31 01:53

I have a bunch of tasks in a MySQL database, and one of the fields is \"deadline date\". Not every task has to have to a deadline date.

I\'d like to use SQL to sort the

4条回答
  •  日久生厌
    2021-01-31 02:33

    SELECT foo, bar, due_date FROM tablename
    ORDER BY CASE ISNULL(due_date, 0)
    WHEN 0 THEN 1 ELSE 0 END, due_date
    

    So you have 2 order by clauses. The first puts all non-nulls in front, then sorts by due date after that

提交回复
热议问题