2013-11-25 3 views
5

우리는 이러한 테이블을 MySQL에 가지고 있습니다 : id - int; 제목 - 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