2013-11-25 52 views
5

我們在MySQL中有這樣的表:id - int; title - varchar; hd - tinyint;源 - tinyint;主動 - tinyint;複雜排序的MySQL查詢

我如何從數據庫中獲得的數據與這樣的排序:

1. hd >= 3 AND source <> 5 
2. hd >= 3 AND source = 5 
3. hd = 2 
4. other, i.e. hd < 2 

請告訴我怎麼做正確和一個SQL查詢?

謝謝。

回答

6
select * from your_table 
order by case when hd >= 3 AND source <> 5 then 1 
       when hd >= 3 AND source = 5 then 2 
       when hd = 2 then 3 
       else 4 
     end 
4

試試這個:

select * 
from table_name 
order by case when hd >= 3 AND source <> 5 then 1 
       when hd >= 3 AND source = 5 then 2 
       when hd = 2 then 3 
       else 4 
     end