2013-11-25 31 views
5

MySQL'de böyle bir tablomuz var: id - int; başlık - varchar; hd - tinyint; kaynak - tinyint; aktif - minik;Karmaşık sıralama ile MySQL sorgusu

Nasıl böyle sıralama ile veritabanından veri alabilirim:

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

nasıl düzgün ve bir sql sorgusu yapmak Lütfen göster?

Teşekkür ederiz.

cevap

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

bu deneyin:

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