MySQL: ORDER BY with empty date '0000-00-00' as last but the rest ASC

瘦欲@ 提交于 2019-12-19 09:29:37

问题


In our database, instead of making empty dates NULL, they are '0000-00-00' (I know, it sucks). How can I order these so that dates that are not '0000-00-00' are first and ordered ASC, and then the empty dates of '0000-00-00' come after?

Thanks!


回答1:


...
ORDER BY CASE WHEN YourDateColumn = '0000-00-00' THEN 2 ELSE 1 END,
         YourDateColumn



回答2:


Try Below :

  SELECT * FROM your_table ORDER BY (date_column='0000-00-00'), date_column ASC

OR

select * from your_table
order by if(date_column='0000-00-00',1,0),date_column;



回答3:


You can use a CASE WHEN http://dev.mysql.com/doc/refman/5.0/en/case-statement.html

... ORDER BY (CASE WHEN date = '0000-00-00' THEN 1 ELSE 0 END) ASC, otherColumns asc, ...



回答4:


ORDER BY (DateColumn = 0)
       , DateColumn


来源:https://stackoverflow.com/questions/9319553/mysql-order-by-with-empty-date-0000-00-00-as-last-but-the-rest-asc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!