[내일배움캠프] SQL 정리4

문인·2025년 10월 24일
0
  1. 오늘 학습 키워드

조건문 if, case

데이터 타입 오류 해결법

  1. 오늘 학습 한 내용을 나만의 언어로 정리하기

substr(addr, 2, 100) 뒤에 숫자 생략시 끝까지 출력

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

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

select addr "원래 주소",
if(addr like '%평택군%', replace(addr, '문곡리', '문가리'), addr) "바뀐 주소"
from food_orders
where addr like '%문곡리%'

select substring(if(email like '%gmail%', replace(email, 'gmail', '@gmail'), email), 10) "이메일 도메인",
count(customer_id) "고객 수",
avg(age) "평균 연령"
from customers
group by 1

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

select order_id,
price,
quantity,
case when quantity=1 then price
when quantity>=2 then price/quantity end "음식 단가"
from food_orders

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

데이터 타입 변환법

--숫자로 변경
cast(if(rating='Not given', '1', rating) as decimal)

--문자로 변경
concat(restaurant_name, '-', cast(order_id as char))

#내일배움캠프 #TIL

0개의 댓글