본문 바로가기

파이썬으로 퀀트 프로그램 만들기 project/MySQL

MySQL - case

728x90

#case

case 식은 경우에 따라 값을 구분할 수 있습니다.

 

사용 방법

case when [조건식1] then [식1]

        when [조건식2] then [식2]

        when [조건식3] then [식3]

        ···

       else [식n]

end

 

else는 위에서 만족하는 조건이 없을때의 반환값입니다.

생략할 수 있지만 명시적으로 작성하는것이 바람직합니다.

 

사용 예시

select col_1, col_2, 
	case when col_2 >= 6000 then 'high'
    	 when col_2 >= 3000 and col_2 < 6000 then 'middle'
         when col_2 < 3000 then 'low'
         else null
end as col2_classify
from nameoftable;