๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ“ ์•Œ๊ณ ๋ฆฌ์ฆ˜/Programmers

[MySQL/PGS] Lv.1 : ์ž๋™์ฐจ ๋Œ€์—ฌ ๊ธฐ๋ก์—์„œ ์žฅ๊ธฐ/๋‹จ๊ธฐ ๋Œ€์—ฌ ๊ตฌ๋ถ„ํ•˜๊ธฐ

728x90

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

๋ฌธ์ œ ์„ค๋ช…

๋‹ค์Œ์€ ์–ด๋Š ์ž๋™์ฐจ ๋Œ€์—ฌ ํšŒ์‚ฌ์˜ ์ž๋™์ฐจ ๋Œ€์—ฌ ๊ธฐ๋ก ์ •๋ณด๋ฅผ ๋‹ด์€ CAR_RENTAL_COMPANY_RENTAL_HISTORY ํ…Œ์ด๋ธ”์ž…๋‹ˆ๋‹ค. CAR_RENTAL_COMPANY_RENTAL_HISTORY ํ…Œ์ด๋ธ”์€ ์•„๋ž˜์™€ ๊ฐ™์€ ๊ตฌ์กฐ๋กœ ๋˜์–ด์žˆ์œผ๋ฉฐ, HISTORY_ID, CAR_ID, START_DATE, END_DATE ๋Š” ๊ฐ๊ฐ ์ž๋™์ฐจ ๋Œ€์—ฌ ๊ธฐ๋ก ID, ์ž๋™์ฐจ ID, ๋Œ€์—ฌ ์‹œ์ž‘์ผ, ๋Œ€์—ฌ ์ข…๋ฃŒ์ผ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.

Column nameTypeNullable
HISTORY_ID INTEGER FALSE
CAR_ID INTEGER FALSE
START_DATE DATE FALSE
END_DATE DATE FALSE

๋ฌธ์ œ

CAR_RENTAL_COMPANY_RENTAL_HISTORY ํ…Œ์ด๋ธ”์—์„œ ๋Œ€์—ฌ ์‹œ์ž‘์ผ์ด 2022๋…„ 9์›”์— ์†ํ•˜๋Š” ๋Œ€์—ฌ ๊ธฐ๋ก์— ๋Œ€ํ•ด์„œ ๋Œ€์—ฌ ๊ธฐ๊ฐ„์ด 30์ผ ์ด์ƒ์ด๋ฉด '์žฅ๊ธฐ ๋Œ€์—ฌ' ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด '๋‹จ๊ธฐ ๋Œ€์—ฌ' ๋กœ ํ‘œ์‹œํ•˜๋Š” ์ปฌ๋Ÿผ(์ปฌ๋Ÿผ๋ช…: RENT_TYPE)์„ ์ถ”๊ฐ€ํ•˜์—ฌ ๋Œ€์—ฌ๊ธฐ๋ก์„ ์ถœ๋ ฅํ•˜๋Š” SQL๋ฌธ์„ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”. ๊ฒฐ๊ณผ๋Š” ๋Œ€์—ฌ ๊ธฐ๋ก ID๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌํ•ด์ฃผ์„ธ์š”.


 

๋‚ ์งœ ํ˜•์‹ : date_format(์ปฌ๋Ÿผ ์ด๋ฆ„, '%Y-%m-%d') as ์ƒˆ ์ปฌ๋Ÿผ ์ด๋ฆ„

๋‚ ์งœ ์ฐจ์ด ๊ตฌํ•˜๊ธฐ (์ผ ์ˆ˜) : datediff(end_date, start_date)

๊ธฐ๊ฐ„ : ๋‚ ์งœ ์ฐจ์ด + 1 = datediff(end_date, start_date) + 1

 

 

๋‚˜์˜ ํ’€์ด

select HISTORY_ID, CAR_ID, DATE_FORMAT(START_DATE, '%Y-%m-%d') as START_DATE, 
    DATE_FORMAT(END_DATE, '%Y-%m-%d')END_DATE, 
    if(DATEDIFF(END_DATE, START_DATE) +1 >= 30,"์žฅ๊ธฐ ๋Œ€์—ฌ","๋‹จ๊ธฐ ๋Œ€์—ฌ") as RENT_TYPE
from CAR_RENTAL_COMPANY_RENTAL_HISTORY
where start_date >= "2022-09-01" and start_date < "2022-10-01"
order by history_id desc;
728x90