[SQL]TIL 쉽고 빠른 SQL 3주차-2(24-04-16)

오정연(DATA_2기)·2024년 4월 16일

SQL

목록 보기
2/12

조건에 따라 포맷을 다르게 변경해야한다면 (IF, CASE)

IF

if(조건, 조건을 충족할 때, 조건을 충족하지 못할 때)

select restaurant_name,
       cuisine_type "원래 음식 타입",
       if(cuisine_type='Korean', '한식', '기타') "음식 타입"
       from food_orders

CASE

case when 조건1 then 값(수식)1
when 조건2 then 값(수식)2
else 값(수식)3 end

select restaurant_name,
       addr,
       case when addr like '%경기도%' then '경기도'
            when addr like '%특별%' or addr like '%광역%' then substring(addr, 1, 5)
            else substring(addr, 1, 2) end "변경된 주소"
from food_orders

Data Type 오류 해결하기

--숫자로 변경

cast(if(rating='Not given', '1', rating) as decimal)

--문자로 변경

concat(restaurant_name, '-', cast(order_id as char))

3주차 실습 문제를 스스로 풀어보면서 그래도 어느정도 이해가 되면서 풀려서 다행인데 이렇게 점점 어려워지다보니 버거워지는거같아 걱정이다.. 할수있다!!!

profile
데이터 새내기

0개의 댓글