SELECT
ROUND(AVG(daily_fee),0) AS average_fee
FROM
car_rental_company_car
WHERE
car_type = 'SUV'
;
ROUND(AVG(daily_fee))
로 적어도 됨SELECT
car_type
, COUNT(*) AS cars
FROM
car_rental_company_car
WHERE
options REGEXP '통풍시트|열선시트|가죽시트'
GROUP BY
car_type
ORDER BY
car_type
;
IN 연산자로는 왜 안 되지?
추가: LIKE 사용
SELECT
car_type
, COUNT(*) AS cars
FROM
car_rental_company_car
WHERE
options LIKE('%통풍시트%')
OR options LIKE('%열선시트%')
OR options LIKE('%가죽시트%')
GROUP BY
car_type
ORDER BY
car_type
;
SELECT
car_type
, COUNT(*) AS cars
FROM
car_rental_company_car
WHERE
INSTR(options, '통풍시트') > 0
OR INSTR(options, '열선시트') > 0
OR INSTR(options, '가죽시트') > 0
GROUP BY
car_type
ORDER BY
car_type
;
MySQL 테이블에 문자열이 포함되어 있는지 확인하는 다양한 방법
- INSTR(str, substr)
- LOCATE(substr, str)
- LOCATE(substr,str) 및 INSTR(str,substr) 함수에서 인수가 전달되는 방식이 다름
- LIKE
- REGEXP
SELECT
flavor
FROM
first_half
ORDER BY
total_order DESC
, shipment_id
;
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
;
SELECT
pt_name
, pt_no
, gend_cd
, age
, COALESCE(TLNO, 'NONE')
FROM
patient
WHERE
age <= 12
AND gend_cd = 'W'
ORDER BY
age DESC
, pt_name
;
SELECT
pt_name
, pt_no
, gend_cd
, age
, (
CASE
WHEN TLNO IS NOT NULL THEN TLNO
ELSE 'NONE'
END
) AS TLNO
FROM
patient
WHERE
age <= 12
AND gend_cd = 'W'
ORDER BY
age DESC
, pt_name
;
SELECT
mcdp_cd AS "진료과 코드"
, COUNT(*) AS "5월예약건수"
FROM
appointment
WHERE
apnt_cncl_ymd IS NULL
AND YEAR(apnt_ymd) = '2022'
AND MONTH(apnt_ymd) = '5'
GROUP BY
mcdp_cd
ORDER BY
COUNT(*)
, mcdp_cd
;
: 예약 취소한 건 카운트 안 하는 거라고 생각하고 풀었는데 아니었음
SELECT
mcdp_cd AS "진료과 코드"
, COUNT(*) AS "5월예약건수"
FROM
appointment
WHERE
YEAR(apnt_ymd) = 2022
AND MONTH(apnt_ymd) = 5
GROUP BY
mcdp_cd
ORDER BY
COUNT(*)
, mcdp_cd
;
WHERE DATE_FORMAT(apnt_ymd, '%Y-%m') = '2022-05'
로 해도 됨-- 백틱 사용
ORDER BY `5월예약건수`, `진료과 코드`
-- 아무것도 없이 사용
ORDER BY 5월예약건수, 진료과코드