这是mytable:
id | state | orderid | name | date
1 | Hold | 121212 | Mike | 2018-04-11
2 | Closed | 121213 | Homer | 2018-04-12
3 | Open | 121214 | Mike | 2018-04-13
4 | Hold | 121215 | Bart | 2018-04-14
5 | Open | 121216 | Lisa | 2018-04-15
6 | Closed | 121217 | Bart | 2018-04-16
7 | Closed | 121218 | Homer | 2018-04-17
我试图通过州(开放,保持,关闭),然后是日期(ASC),如果状态是"关闭",然后是日期(DESC)。所以这样的结果。
id | state | orderid | name | date
3 | Open | 121214 | Mike | 2018-04-13
5 | Open | 121216 | Lisa | 2018-04-15
1 | Hold | 121212 | Mike | 2018-04-11
4 | Hold | 121215 | Bart | 2018-04-14
7 | Closed | 121218 | Homer | 2018-04-17
6 | Closed | 121217 | Bart | 2018-04-16
2 | Closed | 121213 | Homer | 2018-04-12
这是我现在的查询:
SELECT * FROM mytable ORDER BY FIELD(state,'Open','Hold','Closed') ASC, o_date DESC
尝试添加此处给出的示例:Can you add an if statement in ORDER BY? 但我没有得到我想要的结果。
有什么想法吗?
答案 0 :(得分:1)
试试这个..
SELECT * FROM mytable
ORDER BY FIELD(state,'Open','Hold','Closed') ASC,
CASE WHEN state = 'Closed' THEN date END DESC,
date ASC