๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
6๏ธโƒฃ SQL/Problem Solving

[MySQL] Programmers Lv.4 ์ „์ฒด ๋ฌธ์ œ ํ’€์ด

by seolhee2750 2022. 2. 2.
์šฐ์œ ์™€ ์š”๊ฑฐํŠธ๊ฐ€ ๋‹ด๊ธด ์žฅ๋ฐ”๊ตฌ๋‹ˆ
SELECT DISTINCT CART_ID
FROM CART_PRODUCTS
WHERE NAME = 'Milk' AND CART_ID IN (
SELECT CART_ID
FROM CART_PRODUCTS
WHERE NAME = 'Yogurt')
ORDER BY CART_ID

๐Ÿ– DISTINCT ํ‚ค์›Œ๋“œ๋กœ ์ค‘๋ณต ์ œ๊ฑฐ ๊ฐ€๋Šฅ

 

๋ณดํ˜ธ์†Œ์—์„œ ์ค‘์„ฑํ™”ํ•œ ๋™๋ฌผ
SELECT INS.ANIMAL_ID, INS.ANIMAL_TYPE, INS.NAME
FROM ANIMAL_INS AS INS
JOIN ANIMAL_OUTS AS OUTS
ON INS.ANIMAL_ID = OUTS.ANIMAL_ID
WHERE INS.SEX_UPON_INTAKE != OUTS.SEX_UPON_OUTCOME
ORDER BY ANIMAL_ID

 

์ž…์–‘ ์‹œ๊ฐ ๊ตฌํ•˜๊ธฐ(2)
SET @hour := -1;

SELECT (@hour := @hour + 1) AS HOUR, (
    SELECT COUNT(*) FROM ANIMAL_OUTS WHERE @hour = HOUR(DATETIME)) AS COUNT
FROM ANIMAL_OUTS
WHERE @hour < 23

๐Ÿ– SET์„ ํ†ตํ•ด ๋ณ€์ˆ˜ ์„ ์–ธ ๊ฐ€๋Šฅ

๐Ÿ– ๋ณ€์ˆ˜ ์•ž์— @๋ฅผ ๋ถ™์—ฌ์„œ ํ”„๋กœ์‹œ์ €๊ฐ€ ์ข…๋ฃŒ๋˜์–ด๋„ ์œ ์ง€๋˜๋„๋ก ์„ค์ • ๊ฐ€๋Šฅ

๐Ÿ– := ๋ฅผ ํ†ตํ•ด์„œ ๋Œ€์ž… ์—ฐ์‚ฐ ๊ฐ€๋Šฅ

๋Œ“๊ธ€