IT/์ฝ”๋”ฉํ…Œ์ŠคํŠธ

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] MySQL Lv.4 ๋ฌธ์ œํ’€์ด ์ •๋‹ต ๋ชจ์Œ (feat. ์ฝ”๋”ฉํ…Œ์ŠคํŠธ)

zi0_0 2024. 12. 27. 08:15

๋ฌธ์ œ ํ’€์ด ๊ธฐ๊ฐ„ : 24๋…„ 12์›” 27์ผ ~ 

 

์ตœ์‹ ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ ๋ฌธ์ œ ์ˆœ์„œ์ž…๋‹ˆ๋‹ค

 

 

๋ณดํ˜ธ์†Œ์—์„œ ์ค‘์„ฑํ™”ํ•œ ๋™๋ฌผ

SELECT
    I.ANIMAL_ID
    , I.ANIMAL_TYPE
    , I.NAME
FROM ANIMAL_INS AS I
    JOIN ANIMAL_OUTS AS O
    ON I.ANIMAL_ID = O.ANIMAL_ID
WHERE SEX_UPON_INTAKE REGEXP 'Intact'
    AND SEX_UPON_OUTCOME REGEXP 'Spayed|Neutered'
ORDER BY ANIMAL_ID

 

์ž…์–‘ ์‹œ๊ฐ ๊ตฌํ•˜๊ธฐ(2)

WITH RECURSIVE CTE_HOUR AS (
    SELECT 0 AS HOUR
    UNION ALL
    SELECT HOUR + 1
    FROM CTE_HOUR
    WHERE HOUR < 23
    )

SELECT 
    C.HOUR
    , COUNT(A.DATETIME) AS COUNT
FROM CTE_HOUR AS C
    LEFT JOIN ANIMAL_OUTS AS A
    ON C.HOUR = HOUR(A.DATETIME)
GROUP BY HOUR
WITH RECURSIVE
: SQL์—์„œ์˜ ์žฌ๊ท€ ํ€ด๋ฆฌ ๊ธฐ๋ฒ•
  • with์„ ์‚ฌ์šฉํ•ด ๊ฐ€์ƒ์˜ ์ฟผ๋ฆฌ์— ์ €์žฅ
  • ๋ฐ˜๋“œ์‹œ UNION์„ ์‚ฌ์šฉํ•ด์•ผ ํ•จ 
UNION 
: ๋‘ ๊ฐœ ์ด์ƒ์˜ ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ๊ฒฐํ•ฉํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๋ช…๋ น๋ฌธ 
  • UNION : ์ค‘๋ณต๋˜๋Š” ๋ ˆ์ฝ”๋“œ ์ œ๊ฑฐ
  • UNION ALL : ์ค‘๋ณต๋œ ๋ฐ์ดํ„ฐ๋„ ํฌํ•จ 
* ์œ„์˜ UNION ALL ์‚ฌ์šฉ ์ด์œ  
์žฌ๊ท€ CTE๋Š” ์ž๊ธฐ ์ž์‹ ์„ ํ˜ธ์ถœํ•˜์—ฌ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜๋ณต์ ์œผ๋กœ ์ƒ์„ฑํ•จ. 
> ์ค‘๋ณต์„ ์ œ๊ฑฐํ•˜์ง€ ์•Š๊ณ  ๋ชจ๋“  ๋ฐ˜๋ณต๋œ ๊ฒƒ์„ ํฌํ•จํ•ด์•ผ์ง€ ์ •ํ™•ํžˆ ์ž‘๋™ํ•˜๊ธฐ ๋•Œ๋ฌธ์—
UNION์ด ์•„๋‹Œ UNION ALL์„ ์‚ฌ์šฉํ•œ๋‹ค 
โœ๏ธ ํ‹€๋ฆฐ ๋ฌธ์ œ ํ”ผ๋“œ๋ฐฑ
: COUNT(*)๋ฅผ ์จ์„œ ์˜ค๋ฅ˜ ๋ฐœ์ƒ
COUNT(*)๋Š” ๋ชจ๋“  ํ–‰์„ ์นด์šดํŠธํ•˜๋ฏ€๋กœ, ANIMAL_OUTS ํ…Œ์ด๋ธ”์—์„œ ๋งค์นญ๋˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ์—๋„ ๊ทธ ์‹œ๊ฐ„(HOUR)์— ๋Œ€ํ•ด 1๋กœ ๊ณ„์‚ฐ 
> COUNT(A.DATETIME)๋Š” A.DATETIME์ด NULL์ด ์•„๋‹Œ ๊ฒฝ์šฐ๋งŒ ์นด์šดํŠธ

 

์šฐ์œ ์™€ ์š”๊ฑฐํŠธ๊ฐ€ ๋‹ด๊ธด ์žฅ๋ฐ”๊ตฌ๋‹ˆ

SELECT
    CART_ID
FROM CART_PRODUCTS
WHERE NAME IN ('MILK', 'YOGURT')
GROUP BY CART_ID
HAVING COUNT(DISTINCT(NAME)) >= 2
ORDER BY CART_ID

 

์‹ํ’ˆ๋ถ„๋ฅ˜๋ณ„ ๊ฐ€์žฅ ๋น„์‹ผ ์‹ํ’ˆ์˜ ์ •๋ณด ์กฐํšŒํ•˜๊ธฐ

SELECT
    CATEGORY
    , PRICE AS MAX_PRICE
    , PRODUCT_NAME
FROM (
    SELECT *, MAX(PRICE) OVER (PARTITION BY CATEGORY) AS MAX_MONEY
    FROM FOOD_PRODUCT
    ) AS A
WHERE CATEGORY IN ('๊ณผ์ž', '๊ตญ', '๊น€์น˜', '์‹์šฉ์œ ')
    AND PRICE = MAX_MONEY
ORDER BY PRICE DESC

 

5์›” ์‹ํ’ˆ๋“ค์˜ ์ด๋งค์ถœ ์กฐํšŒํ•˜๊ธฐ

SELECT
    P.PRODUCT_ID
    , P.PRODUCT_NAME
    , SUM(PRICE * AMOUNT) AS TOTAL_SALES
FROM 
    FOOD_PRODUCT AS P
    JOIN FOOD_ORDER AS O
    ON P.PRODUCT_ID = O.PRODUCT_ID
WHERE PRODUCE_DATE LIKE ('2022-05%')
GROUP BY 1 
ORDER BY TOTAL_SALES DESC, PRODUCT_ID

 

์„œ์šธ์— ์œ„์น˜ํ•œ ์‹๋‹น ๋ชฉ๋ก ์ถœ๋ ฅํ•˜๊ธฐ

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 ADDRESS LIKE '์„œ์šธ%'
GROUP BY 1, 2, 3, 4, 5
ORDER BY SCORE DESC, FAVORITES DESC

 

๊ทธ๋ฃน๋ณ„ ์กฐ๊ฑด์— ๋งž๋Š” ์‹๋‹น ๋ชฉ๋ก ์ถœ๋ ฅํ•˜๊ธฐ

SELECT
    M.MEMBER_NAME
    , R.REVIEW_TEXT
    , DATE_FORMAT(R.REVIEW_DATE, '%Y-%m-%d') AS REVIEW_DATE
FROM 
    REST_REVIEW AS R
    JOIN MEMBER_PROFILE AS M
    ON R.MEMBER_ID = M.MEMBER_ID
WHERE M.MEMBER_ID = (
                    SELECT MEMBER_ID
                    FROM REST_REVIEW
                    GROUP BY MEMBER_ID
                    ORDER BY COUNT(*) DESC 
                    LIMIT 1
                    )
ORDER BY 3, 2

 

๋…„, ์›”, ์„ฑ๋ณ„ ๋ณ„ ์ƒํ’ˆ ๊ตฌ๋งค ํšŒ์› ์ˆ˜ ๊ตฌํ•˜๊ธฐ

SELECT
    YEAR(S.SALES_DATE) AS YEAR
    , MONTH(S.SALES_DATE) AS MONTH
    , I.GENDER
    , COUNT(DISTINCT S.USER_ID) AS USERS
FROM 
    USER_INFO AS I
    JOIN ONLINE_SALE AS S
    ON I.USER_ID = S.USER_ID
WHERE I.GENDER IS NOT NULL
GROUP BY 1, 2, 3
ORDER BY 1, 2, 3

 

์˜คํ”„๋ผ์ธ/์˜จ๋ผ์ธ ํŒ๋งค ๋ฐ์ดํ„ฐ ํ†ตํ•ฉํ•˜๊ธฐ

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%') -- ONLINE_SALE ํ…Œ์ด๋ธ”์— ์ ์šฉ 
UNION 
SELECT 
    DATE_FORMAT(SALES_DATE, '%Y-%m-%d') AS SALES_DATE
    , PRODUCT_ID
    , NULL
    , SALES_AMOUNT
FROM OFFLINE_SALE
WHERE SALES_DATE LIKE ('2022-03%') -- ์ „์ฒด ํ…Œ์ด๋ธ”์ด ์•„๋‹Œ, OFFLINE_SALE ํ…Œ์ด๋ธ”์— ์ ์šฉ 
ORDER BY 1, 2, 3 -- ๋ณ‘ํ•ฉ๋œ ์ „์ฒด ๊ฒฐ๊ณผ์— ์ ์šฉ๋จ

* ์œ„์˜ ์ฟผ๋ฆฌ์—์„œ WHERE์ ˆ์€ SELECT ๊ตฌ๋ฌธ์˜ ๊ฒฐ๊ณผ๋กœ ์ œํ•œ๋˜์–ด ์ „์ฒด ํ…Œ์ด๋ธ”์— ์ ์šฉ๋˜์ง€ ์•Š์ง€๋งŒ,
   ORDER BY์€ ๋ณ‘ํ•ฉ๋œ ๊ฒฐ๊ณผ์— ์ ์šฉ๋œ๋‹ค. 

UNION 
- UNION : ์ค‘๋ณต๋œ ํ–‰์„ ์ œ๊ฑฐํ•˜๊ณ  ๋ณ‘ํ•ฉ
- UNION ALL : ์ค‘๋ณต๋œ ํ–‰์„ ํฌํ•จํ•˜์—ฌ ๋ณ‘ํ•ฉ 

* WHERE ์ ˆ ์ ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• 2๊ฐ€์ง€ 

* ๊ฐ๊ฐ ์กฐ๊ฑด ์ ์šฉํ•˜๊ณ  union
SELECT column1, column2 FROM table1
WHERE condition1
UNION
SELECT column1, column2 FROM table2
WHERE condition2;

* union ๋ณ‘ํ•ฉ ํ›„ ์กฐ๊ฑด ์ ์šฉ 
SELECT * FROM (
    SELECT column1, column2 FROM table1
    UNION
    SELECT column1, column2 FROM table2
) AS merged_table
WHERE final_condition;

 

์ทจ์†Œ๋˜์ง€ ์•Š์€ ์ง„๋ฃŒ ์˜ˆ์•ฝ ์กฐํšŒํ•˜๊ธฐ

o

 

์ฃผ๋ฌธ๋Ÿ‰์ด ๋งŽ์€ ์•„์ด์Šคํฌ๋ฆผ๋“ค ์กฐํšŒํ•˜๊ธฐ

o

 

์ €์ž ๋ณ„ ์นดํ…Œ๊ณ ๋ฆฌ ๋ณ„ ๋งค์ถœ์•ก ์ง‘๊ณ„ํ•˜๊ธฐ

o

 

์ž๋™์ฐจ ๋Œ€์—ฌ ๊ธฐ๋ก ๋ณ„ ๋Œ€์—ฌ ๊ธˆ์•ก ๊ตฌํ•˜๊ธฐ

o

 

ํŠน์ • ๊ธฐ๊ฐ„๋™์•ˆ ๋Œ€์—ฌ ๊ฐ€๋Šฅํ•œ ์ž๋™์ฐจ๋“ค์˜ ๋Œ€์—ฌ๋น„์šฉ ๊ตฌํ•˜๊ธฐ

o

 

FrontEnd ๊ฐœ๋ฐœ์ž ์ฐพ๊ธฐ

o

 

์–ธ์–ด๋ณ„ ๊ฐœ๋ฐœ์ž ๋ถ„๋ฅ˜ํ•˜๊ธฐ

o

 

์—ฐ๊ฐ„ ํ‰๊ฐ€์ ์ˆ˜์— ํ•ด๋‹นํ•˜๋Š” ํ‰๊ฐ€ ๋“ฑ๊ธ‰ ๋ฐ ์„ฑ๊ณผ๊ธˆ ์กฐํšŒํ•˜๊ธฐ

o

 

ํŠน์ • ์„ธ๋Œ€์˜ ๋Œ€์žฅ๊ท  ์ฐพ๊ธฐ

o

 

 

Copy