티스토리 뷰

Lv2. 조건에 맞는 도서와 저자 리스트 출력하기

문제

- 경제 카테고리에 속하는 도서id, 저자명, 출판일 리스트 출력 => INNER JOIN으로 AUTHOR 테이블과 BOOK 테이블 연결

- 출판일 기준 오름차순 정렬  =>ORDER BY

- 데이트 포맷이 예시와 동일해야 정답 처리 => DATE_FORMAT()

 

풀이1 : FLOOR 이용

SELECT BOOK_ID, AUTHOR_NAME, DATE_FORMAT(PUBLISHED_DATE,'%Y-%m-%d') AS PUBLISHED_DATE
FROM BOOK AS B
INNER JOIN AUTHOR AS A ON B.AUTHOR_ID = A.AUTHOR_ID
WHERE CATEGORY = "경제"
ORDER BY PUBLISHED_DATE;

- 그냥 published_date쓰면 시간까지 출력돼서 예시형태와 다름! -> date_format을 이용해 format 정해주기

* INNER JOIN 문법 정리

SELECT <열 목록>
FROM <첫 번째 테이블>
    INNER JOIN <두 번째 테이블>
    ON <조인 조건>
[WHERE 검색 조건]

 

- 내부조인을 이용해 두 테이블 연결! ON에서 조건을 정의해주기!

 

* DATE_FORMAT 문법 정리

DATE_FORMAT(<열이름>, <원하는 형태>)

이때, 형식을 어떻게 정의해주느냐가 중요!

ex) 2020-01-01 = '%Y-%m-%d'

%Y : 4자리 연도

%m : 숫자 월 -> 대문자로 하면 영어로 출력

%d : 숫자 날짜 -> 대문자로 하면 1st, 2nd 형식으로 출력

 

참고) MySQL DATE_FORMAT() Function (w3schools.com)

%a Abbreviated weekday name (Sun to Sat)
%b Abbreviated month name (Jan to Dec)
%c Numeric month name (0 to 12)
%D Day of the month as a numeric value, followed by suffix (1st, 2nd, 3rd, ...)
%d Day of the month as a numeric value (01 to 31)
%e Day of the month as a numeric value (0 to 31)
%f Microseconds (000000 to 999999)
%H Hour (00 to 23)
%h Hour (00 to 12)
%I Hour (00 to 12)
%i Minutes (00 to 59)
%j Day of the year (001 to 366)
%k Hour (0 to 23)
%l Hour (1 to 12)
%M Month name in full (January to December)
%m Month name as a numeric value (00 to 12)
%p AM or PM
%r Time in 12 hour AM or PM format (hh:mm:ss AM/PM)
%S Seconds (00 to 59)
%s Seconds (00 to 59)
%T Time in 24 hour format (hh:mm:ss)
%U Week where Sunday is the first day of the week (00 to 53)
%u Week where Monday is the first day of the week (00 to 53)
%V Week where Sunday is the first day of the week (01 to 53). Used with %X
%v Week where Monday is the first day of the week (01 to 53). Used with %x
%W Weekday name in full (Sunday to Saturday)
%w Day of the week where Sunday=0 and Saturday=6
%X Year for the week where Sunday is the first day of the week. Used with %V
%x Year for the week where Monday is the first day of the week. Used with %v
%Y Year as a numeric, 4-digit value
%y Year as a numeric, 2-digit value