MYSQL按案例排序问题

时间:2010-12-03 15:06:34

标签: mysql

我有一个这样的数据库:

-------------------------------------------------------------------
|   id_one   |   id_two   |   timestamp_one   |   timestamp_two   |
-------------------------------------------------------------------
|     27     |     35     |        9:30       |        NULL       |
-------------------------------------------------------------------
|     35     |     27     |        NULL       |        9:35       |
-------------------------------------------------------------------
|     27     |     35     |        9:34       |        NULL       |
-------------------------------------------------------------------
|     35     |     27     |        NULL       |        9:33       |
-------------------------------------------------------------------

我需要拉4行

ORDER BY 'timestamp_one' if 'id_one'=27 or 
ORDER BY 'timestamp_two' if 'id_one'=27

这是我现在的声明:

SELECT * FROM tablename 
WHERE id_one=27 OR id_two=27 
ORDER BY 
  CASE WHEN id_one=27 THEN timestamp_one END DESC, 
  CASE WHEN id_two=27 THEN timestamp_two END DESC

这很好用就是输出:

-------------------------------------------------------------------
|   id_one   |   id_two   |   timestamp_one   |   timestamp_two   |
-------------------------------------------------------------------
|     27     |     35     |        9:30       |        NULL       |
-------------------------------------------------------------------
|     27     |     35     |        9:34       |        NULL       |
-------------------------------------------------------------------
|     35     |     27     |        NULL       |        9:33       |
-------------------------------------------------------------------
|     35     |     27     |        NULL       |        9:35       |
-------------------------------------------------------------------

但是我需要两个时间戳列来排序,就像它们一样,所以它会像这样订购:

-------------------------------------------------------------------
|   id_one   |   id_two   |   timestamp_one   |   timestamp_two   |
-------------------------------------------------------------------
|     27     |     35     |        9:30       |        NULL       |
-------------------------------------------------------------------
|     35     |     27     |        NULL       |        9:33       |
-------------------------------------------------------------------
|     27     |     35     |        9:34       |        NULL       |
-------------------------------------------------------------------
|     35     |     27     |        NULL       |        9:35       |
-------------------------------------------------------------------

我希望这是有道理的。本质上,我试图有两个特定于WHERE条件的ORDER BY列。然后,一旦为该行选择了正确的ORDER BY列,它就会按时间顺序对ROWS进行排序。

3 个答案:

答案 0 :(得分:42)

SELECT id_one, id_two, timestamp_one, timestamp_two      
FROM tablename 
WHERE id_one = 27 
    OR id_two = 27 
ORDER BY 
    CASE 
        WHEN id_one=27 THEN timestamp_one 
        WHEN id_two=27 THEN timestamp_two 
    END DESC 

答案 1 :(得分:11)

SELECT 
   id_one,
   id_two,
   (CASE 
      WHEN id_one=27 THEN timestamp_one
      WHEN id_two=27 THEN timestamp_two    
   END) as timestamp
FROM tablename
ORDER BY timestamp DESC

答案 2 :(得分:1)

SELECT * FROM tablename 
WHERE id_one=27 OR id_two=27 
ORDER BY id ASC, timestamp_one DESC