[SQL] 조건에 맞는 도서 리스트 출력하기

문지영·2023년 4월 26일
0

MySQL

목록 보기
4/7

문제

조건에 맞는 도서 리스트 출력하기

BOOK 테이블에서 2021년에 출판된 '인문' 카테고리에 속하는 도서 리스트를 찾아서 도서 ID(BOOK_ID), 출판일 (PUBLISHED_DATE)을 출력하는 SQL문을 작성해주세요.
결과는 출판일을 기준으로 오름차순 정렬해주세요.

배운 것

MySQL date

  1. DATE_FORMAT(date, format)
    Formats the date value according to the format string. If either argument is NULL, the function returns NULL.
    %Y: 2023, %y:23 을 의미

  2. YEAR(date)
    Returns the year for date, in the range 1000 to 9999, or 0 for the “zero” date. Returns NULL if date is NULL.

  3. DATE(expr)
    Extracts the date part of the date or datetime expression expr. Returns NULL if expr is NULL.

코드

SELECT 
    book_id, 
    DATE_FORMAT(published_date,'%Y-%m-%d') 
FROM book 
WHERE YEAR(published_date)='2021'
    AND category='인문'
ORDER BY published_date;
profile
BeHappy

0개의 댓글