我有一张桌子
Id ordertype orderdate
1 open 09/01/2014
2 pending 09/01/2014
3 open 09/30/2014
4 closed 06/01/2014
5 closed 9/15/20143
我创建了一个mysql查询,按字段排序我想要的。
select * from table order by FIELD(ordertype, 'pending', 'open', 'closed'), orderdate
将按照
对上表进行排序Id ordertype orderdate
2 pending 09/01/2014
3 open 09/30/2014
1 open 09/01/2014
5 closed 09/01/2014
4 closed 06/01/2014
先挂起,打开秒,然后在底部关闭。
但我想做的是根据今天的日期关闭。意思ID#5将在2014年1月9日之前排在最前面,然后它会在其他已关闭的订单下降到最低点。
Id ordertype orderdate
5 closed 09/01/2014
2 pending 09/01/2014
3 open 09/30/2014
1 open 09/01/2014
4 closed 06/01/2014
感谢您的帮助
答案 0 :(得分:0)
使用它:
select * from table order by FIELD(ordertype, 'pending', 'open', 'closed'), orderdate, orderdate desc
答案 1 :(得分:0)
您可以使用案例表达式,例如
select *
from table1
order by
case when ordertype = 'closed' and orderdate > curdate() then 0 else 1 end
, FIELD(ordertype, 'pending', 'open', 'closed')
, orderdate
nb:不确定您是否需要> curdate()或> = curdate()