데이터베이스/문제 풀이

[프로그래머스][MySQL] 자동차 대여 기록에서 장기/단기 대여 구분하기

수수다 2026. 7. 6. 19:23

https://school.programmers.co.kr/learn/courses/30/lessons/151138

 

프로그래머스

SW개발자를 위한 평가, 교육의 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

 

select 
    history_id,
    car_id,
    date_format(start_date, '%Y-%m-%d') as start_date,
    date_format(end_date, '%Y-%m-%d') as end_date,
    case
        when datediff(end_date, start_date) + 1 >= 30 then '장기 대여'
        else '단기 대여'
    end as rent_type
from car_rental_company_rental_history
where start_date between '2022-09-01' and '2022-09-30'
order by history_id desc

 

기간의 일수를 구해야할 때
두 날짜를 빼는 메서드
DATEDIFF(끝, 시작)