[英]MySQL - Order by date ASC and DESC, depending on other field
I have these rows in a table... 我在表中有这些行...
id task_type task_date
1 1 01/15/2010
2 2 01/18/2010
3 1 01/18/2010
4 2 01/16/2010
6 1 01/16/2010
8 1 01/17/2010
9 2 01/17/2010
and I want to order them in a particular way: when task_type=1 order by ASC and when task_type=2 order by DESC 我想以一种特殊的方式排序它们:当task_type = 1由ASC排序,而task_type = 2由DESC排序
id task_type task_date
1 1 01/15/2010
6 1 01/16/2010
8 1 01/17/2010
3 1 01/18/2010
2 2 01/18/2010
9 2 01/17/2010
4 2 01/16/2010
Is this even possible? 这有可能吗? I've been looking for conditional queries and all i can get is a syntax error... 我一直在寻找条件查询,我所能得到的只是语法错误...
This is the query that i cant make it work properly: 这是我无法使其正常工作的查询:
SELECT id, task_type, task_date
From Tasks
ORDER BY CASE WHEN task_type=1 THEN
task_type ASC, task_date ASC, hour ASC, priority DESC, id ASC
WHEN task_type=2 THEN
task_type ASC, task_date DESC, hour ASC, priority DESC, id ASC
END
I'm not sure what estado
is, but your logic is seems to be: 我不确定estado
是什么,但是您的逻辑似乎是:
ORDER BY task_type,
(CASE WHEN task_type = 1 THEN task_date END) ASC,
(CASE WHEN task_type = 1 THEN hour END) ASC,
(CASE WHEN task_type = 1 THEN priority END) DESC,
(CASE WHEN task_type = 1 THEN id END) ASC,
(CASE WHEN task_type = 2 THEN task_date END) DESC,
(CASE WHEN task_type = 2 THEN hour END) ASC,
(CASE WHEN task_type = 2 THEN priority END) DESC,
(CASE WHEN task_type = 2 THEN id END) ASC
This can be simplified to: 可以简化为:
ORDER BY task_type,
(CASE WHEN task_type = 1 THEN task_date END) ASC,
(CASE WHEN task_type = 2 THEN task_date END) DESC,
hour ASC, priority DESC, id ASC
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.