问题
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