Growth ๐ŸŒณ/Practice ๐Ÿ’ป

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์ฆ๊ฒจ์ฐพ๊ธฐ๊ฐ€ ๊ฐ€์žฅ ๋งŽ์€ ์‹๋‹น ์ •๋ณด ์ถœ๋ ฅํ•˜๊ธฐ(2์ฐจ ํ’€์ด)

์ธ” 2023. 1. 12. 14:09

๐Ÿ“ข ๋ณธ ํฌ์ŠคํŒ…์€ Growth/Practice ๊ฒŒ์‹œ๊ธ€ (https://sy038680.tistory.com/239) ๋ฌธ์ œ์— ๋Œ€ํ•œ 2์ฐจ ํ’€์ด์ž…๋‹ˆ๋‹ค.


๋‹ต๋งŒ ์ƒ๊ฐํ•˜๋‹ค๊ฐ€ ๋ƒ…๋‹ค UNION ์œผ๋กœ ํ’€์—ˆ๋‹ค๊ฐ€

๋ฌธ์ œ ๊ตฌ๋ถ„์— GROUP BY ๊ด€๋ จ ๋ฌธ์ œ๋ผ๋Š” ๊ฒƒ์„ ๋ณด๊ณ  ๋‹ค์‹œ ํ’€์ด๋ฅผ ํ•ด๋ณด๊ธฐ๋กœ ํ–ˆ๋‹ค.

WHERE์ ˆ์—์„œ FOOD_TYPE๋ณ„๋กœ FAVORITES์˜ ์ตœ๋Œ€๊ฐ’์„ ์ฐพ๋Š” ๊ฒƒ์— () IN ์„ ํ™œ์šฉํ•˜๋ฉด ๊ฐ„๋‹จํ•˜๊ฒŒ ํ’€ ์ˆ˜ ์žˆ์—ˆ๋‹ค.

SELECT FOOD_TYPE, REST_ID, REST_NAME, FAVORITES
 FROM REST_INFO
 WHERE (FOOD_TYPE, FAVORITES) IN (SELECT FOOD_TYPE, MAX(FAVORITES)
                                  FROM REST_INFO
                                  GROUP BY 1)
 ORDER BY 1 DESC

WHERE์ ˆ์—์„œ FOOD_TYPE๋ณ„๋กœ ์ฆ๊ฒจ์ฐพ๊ธฐ ์ตœ๋Œ€๊ฐ’์„ ์ฐพ์•„์คŒ์œผ๋กœ์„œ

SELECT์ ˆ์—์„œ FOOD_TYPE๋ณ„๋กœ ์ฆ๊ฒจ์ฐพ๊ธฐ ์ˆ˜๊ฐ€ ์ผ์น˜ํ•˜๋Š” ์‹๋‹น ID์™€ ์ด๋ฆ„๊นŒ์ง€ ์ถœ๋ ฅ์ด ๊ฐ€๋Šฅํ•ด์กŒ๋‹ค.