본문 바로가기

TOOL/MySQL

MySQL - CASE

반응형
case

 

 

간단한 조건문을 작성할때 사용하는 case( )

 

 

 

 

released_year가 2000년 이후에 나온 책들은 modern 이라고 하고,

 

그렇지 않은 책들은 Old 라고 새로운 컬럼을 만들어서 가져오세요

select *,
case
when released_year >= 2000 then 'Modern'
else 'Old'
    end as 'Genre'
from books;

 

 

 

 

 

 

stock_quantity 가 0~50 사이면 *(별표 한개) 

 

stock_quantity 가 51~100 사이면 **(별표 두개)

 

stock_quantity 가 그 외에는 ***(별표 세개)

select *,
	case
		when stock_quantity between 0 and 50 then '*'
		when stock_quantity between 51 and 100 then '**'
        else '***'
	end as star
from books;

 

반응형