4

所以我得到了这张桌子:

+----+---------------------+-----+---------------- +
| 身份证 | 何时 | 关闭 | (其他..东西)|
+----+---------------------+-----+---------------- +
| 1 | 2012-09-17 17:00:00 | 0 | 任何东西1 |
| 2 | 2012-09-17 18:00:00 | 0 | 什么2 |
| 3 | 2012-08-31 21:00:00 | 1 | blabla321 |
| 4 | 2012-08-31 18:30:00 | 1 | blab32121 |
+----+---------------------+-----+---------------- +

我想选择所有键,但是:

  • off = 0 的键应该在 ASC 时排序,并且应该首先显示
  • 带 off = 1 的键应按 DESC 时排序,并应显示在(最后)之后

我试过这样的事情:

(SELECT * FROM `table` WHERE `off` = 0 ORDER BY `when` ASC)
UNION
(SELECT * FROM `table` WHERE `off` = 1 ORDER BY `when` DESC)

但它不起作用。

另请查看:http: //i.imgur.com/81Hzq.jpg

4

3 回答 3

2

试试这个:

(SELECT * FROM table WHERE off = 0 ORDER BY `when` ASC) 
UNION 
(SELECT * FROM table WHERE off = 1 ORDER BY `when` DESC);

我认为 WHEN 是一个关键字,必须引用。

于 2012-09-17T15:21:24.203 回答
1
SELECT * 
FROM table 
ORDER BY CASE WHEN off = 0 THEN `when` END ASC,
         CASE WHEN off = 1 THEN `when` END DESC 
于 2012-09-17T15:21:02.583 回答
1
select *
from `table`
order by `off`,
    case `off`
        when 0 then timestampdiff(second, current_timestamp, `when`)
        when 1 then timestampdiff(second, `when`, current_timestamp)
    end
于 2012-09-17T22:22:26.233 回答