SELECT
B.TITLE,
B.BOARD_ID,
R.REPLY_ID,
R.WRITER_ID,
R.CONTENTS,
DATE_FORMAT(R.CREATED_DATE, '%Y-%m-%d') AS CREATED_DATE
FROM USED_GOODS_BOARD AS B
JOIN USED_GOODS_REPLY AS R
ON B.BOARD_ID = R.BOARD_ID
WHERE R.CREATED_DATE AND B.CREATED_DATE between "2022-10-01" and "2022-10-31"
ORDER BY R.CREATED_DATE ASC, B.TITLE ASC
DATE_FORMAT(포맷을 변경할 컬럼명, 변경하고 싶은 날자 타입)
SELECT DATE_FORMAT('20201224', '%Y-%m-%d')
: Y만 대문자
M 대문자로 할 경우 월 이름(January...)으로 출력,
D 대문자로 할 경우 일자가 1st, 2nd... 이런 식으로 출력
ORDER BY 뒤에 기준을 순서대로 나열, 쉼표로 구분
ORDER BY level DESC, name ASC
JOIN 컬럼명 AS C ON A.컬럼1 = C.컬럼2
SELECT
FACTORY_ID,
FACTORY_NAME,
ADDRESS
FROM FOOD_FACTORY
WHERE ADDRESS LIKE "강원도%"
ORDER BY FACTORY_ID
강아지로 시작하는 데이터 select * from dog where title like '강아지%';
강아지로 끝나는 데이터 select * from dog where title like '%강아지';
강아지가 들어가는 데이터 검색 select * from dog where title like '%강아지%';
SELECT
I.REST_ID,
I.REST_NAME,
I.FOOD_TYPE,
I.FAVORITES,
I.ADDRESS,
ROUND(AVG(R.REVIEW_SCORE), 2) AS SCORE
FROM REST_INFO AS I
JOIN REST_REVIEW AS R
ON I.REST_ID = R.REST_ID
WHERE I.ADDRESS LIKE "서울%"
GROUP BY I.REST_ID
ORDER BY SCORE DESC, I.FAVORITES DESC
값들의 '집합'을 계산해서 '하나'의 값을 리턴하는 것.
COUNT(*)
를 제외하고 집계함수는 NULL을 무시한다.CEIL(값)
➡️ 무조건 정수값으로 올림FLOOR(값)
➡️ 무조건 정수값으로 내림TRUNCATE(값, 자리수)
➡️ 특정 자리수까지 버려줌ROUND(값)
/ ROUND(값, 자리수)
SELECT
DR_NAME,
DR_ID,
MCDP_CD,
DATE_FORMAT(HIRE_YMD, '%Y-%m-%d') AS HIRE_YMD
FROM DOCTOR
WHERE MCDP_CD = 'CS' OR MCDP_CD = 'GS'
ORDER BY HIRE_YMD DESC, DR_NAME
SELECT
BOOK_ID,
DATE_FORMAT(PUBLISHED_DATE, '%Y-%m-%d') AS PUBLISHED_DATE
FROM BOOK
WHERE CATEGORY = '인문'
AND DATE_FORMAT(PUBLISHED_DATE, '%Y') = '2021'
ORDER BY PUBLISHED_DATE
# 2023년만 검색
WHERE DATE_FORMAT(날짜, '%Y') = '2023'
# 3월만 검색
WHERE DATE_FORMAT(날짜, '%m') = '03'
OR
WHERE DATE_FORMAT(날짜, '%M') = 'March'
SELECT
ROUND(AVG(DAILY_FEE)) AS AVERAGE_FEE
FROM CAR_RENTAL_COMPANY_CAR
GROUP BY CAR_TYPE
HAVING CAR_TYPE = 'SUV'
SELECT
H.FLAVOR
FROM FIRST_HALF AS H
JOIN ICECREAM_INFO AS I
ON H.FLAVOR = I.FLAVOR
WHERE H.TOTAL_ORDER > 3000
AND I.INGREDIENT_TYPE LIKE 'fruit%'
ORDER BY H.TOTAL_ORDER DESC
SELECT
PT_NAME,
PT_NO,
GEND_CD,
AGE,
CASE WHEN TLNO IS NULL THEN 'NONE'
ELSE TLNO
END AS TLNO
FROM PATIENT
WHERE AGE<=12 AND GEND_CD='W'
ORDER BY AGE DESC, PT_NAME ASC
또는
SELECT
PT_NAME,
PT_NO,
GEND_CD,
AGE,
IFNULL(TLNO, 'NONE') TLNO
FROM PATIENT
WHERE AGE<=12 AND GEND_CD='W'
ORDER BY AGE DESC, PT_NAME ASC
IFNULL(컬럼명, NULL인 경우 표기할 값) 표기할 컬럼명
SELECT IFNULL(환자번호, 'NONE') 환자번호
FROM PATIENT
# 환자번호가 NULL인 경우 NONE으로 표기
SELECT
FLAVOR
FROM FIRST_HALF
ORDER BY TOTAL_ORDER DESC, SHIPMENT_ID ASC
SELECT *
FROM ANIMAL_INS
ORDER BY ANIMAL_ID
SELECT
USER_ID,
PRODUCT_ID
FROM ONLINE_SALE
GROUP BY USER_ID, PRODUCT_ID
HAVING COUNT(*) >= 2
ORDER BY USER_ID, PRODUCT_ID DESC
HAVING 절에는 집계함수 사용이 가능함
➡️ 일단 GROUP BY로 묶은 뒤 HAVING 집계함수~
SELECT
NAME,
DATETIME
FROM ANIMAL_INS
ORDER BY ANIMAL_ID DESC
역순이라는 게... 내림차순인가 봅니다.
SELECT DATE_FORMAT(SALES_DATE, "%Y-%m-%d") AS SALES_DATE,
PRODUCT_ID,
USER_ID,
SALES_AMOUNT
FROM ONLINE_SALE
WHERE SALES_DATE LIKE "2022-03%"
UNION ALL
SELECT
DATE_FORMAT(SALES_DATE, "%Y-%m-%d") AS SALES_DATE,
PRODUCT_ID,
NULL AS USER_ID,
SALES_AMOUNT
FROM OFFLINE_SALE
WHERE SALES_DATE LIKE "2022-03%"
order by sales_date, product_id, user_id
UNION
-> 중복 컬럼 제거UNION ALL
-> 중복 컬럼 SELECT ANIMAL_ID, NAME
FROM ANIMAL_INS
WHERE INTAKE_CONDITION LIKE "%Sick%"
SELECT ANIMAL_ID, NAME
FROM ANIMAL_INS
WHERE INTAKE_CONDITION NOT LIKE "%Aged%"
WHERE 컬럼명 NOT IN (제외할 값 1, 2, 3...)
ORDER BY 정렬 후 마지막줄에 LIMIT n
걸어주기
SELECT NAME
FROM ANIMAL_INS
ORDER BY DATETIME
LIMIT 1