2013-11-25 22 views
5

Abbiamo una tabella di questo tipo in MySQL: id-int; titolo: varchar; hd - tinyint; fonte - tinyint; attivo - tinyint;Query MySQL con ordinamento complesso

Come faccio a ottenere i dati dal database con tale ordinamento:

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

favore, mi mostri come farlo correttamente e domanda una sql?

Grazie.

risposta

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

Prova questo:

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