MYSQL ORDER BY CASE Issue

前端 未结 3 1050
囚心锁ツ
囚心锁ツ 2020-11-27 21:44

I have a database like this:

-------------------------------------------------------------------
|   id_one   |   id_two   |   timestamp_one   |   timestamp_         


        
相关标签:
3条回答
  • 2020-11-27 22:02
    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 
    
    0 讨论(0)
  • 2020-11-27 22:10
    SELECT * FROM tablename 
    WHERE id_one=27 OR id_two=27 
    ORDER BY id ASC, timestamp_one DESC
    
    0 讨论(0)
  • 2020-11-27 22:14
    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
    
    0 讨论(0)
提交回复
热议问题