SQL订单由2个日期字段组成

时间:2016-02-23 10:16:03

标签: mysql sql

我在MySQL中运行一个select查询:

SELECT * 
FROM table1 
ORDER BY IF(duedate = '0000-00-00 00:00:00', datetime_added, duedate) ASC

它的排序ASC但是它将所有0000-00-00 00:00:00的行放在顶部,但这些行应该在底部

如何按以下顺序订购查询:

duedate ASC
datetime_added ASC
sequence DESC

2 个答案:

答案 0 :(得分:1)

试试这个:

SELECT * 
from table1 
ORDER BY IF(duedate = '0000-00-00 00:00:00', '9999-12-31', duedate),         
         datetime_added ASC,
         sequence DESC

答案 1 :(得分:0)

您可以通过以下方式执行此操作:

SELECT * from table1 
ORDER BY 
duedate ASC,
duedate = '0000-00-00 00:00:00',
datetime_added ASC,
sequence DESC