Growth ๐ŸŒณ/Practice ๐Ÿ’ป

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์กฐ๊ฑด์— ๋งž๋Š” ์‚ฌ์šฉ์ž ์ •๋ณด ์กฐํšŒํ•˜๊ธฐ

์ธ” 2023. 3. 23. 17:57

๐Ÿ“ข ๋ณธ ํฌ์ŠคํŒ…์— ํ™œ์šฉ๋˜๋Š” ๊ธฐ๋ณธ ๋ฌธ์ œ ๋ฐ ์ž๋ฃŒ ์ถœ์ฒ˜๋Š”

       ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต,   

      https://school.programmers.co.kr/learn/challenges?page=1&languages=mysql%2Coracle์ž„์„ ๋ฐํž™๋‹ˆ๋‹ค.

      · ๋ณธ ๋ฌธ์ œ https://school.programmers.co.kr/learn/courses/30/lessons/164670


โ–  ๋ฌธ์ œ

๋ฌธ์ œ ์„ค๋ช…

๋‹ค์Œ์€ ์ค‘๊ณ  ๊ฑฐ๋ž˜ ๊ฒŒ์‹œํŒ ์ •๋ณด๋ฅผ ๋‹ด์€ USED_GOODS_BOARD ํ…Œ์ด๋ธ”๊ณผ ์ค‘๊ณ  ๊ฑฐ๋ž˜ ๊ฒŒ์‹œํŒ ์ฒจ๋ถ€ํŒŒ์ผ ์ •๋ณด๋ฅผ ๋‹ด์€ USED_GOODS_USER ํ…Œ์ด๋ธ”์ž…๋‹ˆ๋‹ค.  

 USED_GOODS_BOARD ํ…Œ์ด๋ธ”์€ ๋‹ค์Œ๊ณผ ๊ฐ™์œผ๋ฉฐ BOARD_ID, WRITER_ID, TITLE, CONTENTS, PRICE, CREATED_DATE, STATUS, VIEWS๋Š” ๊ฒŒ์‹œ๊ธ€ ID, ์ž‘์„ฑ์ž ID, ๊ฒŒ์‹œ๊ธ€ ์ œ๋ชฉ, ๊ฒŒ์‹œ๊ธ€ ๋‚ด์šฉ, ๊ฐ€๊ฒฉ, ์ž‘์„ฑ์ผ, ๊ฑฐ๋ž˜์ƒํƒœ, ์กฐํšŒ์ˆ˜๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

Column name Type Nullable
BOARD_ID VARCHAR(5) FALSE
WRITER_ID VARCHAR(50) FALSE
TITLE VARCHAR(100) FALSE
CONTENTS VARCHAR(1000) FALSE
PRICE NUMBER FALSE
CREATED_DATE DATE FALSE
STATUS VARCHAR(10) FALSE
VIEWS NUMBER FALSE

USED_GOODS_USER ํ…Œ์ด๋ธ”์€ ๋‹ค์Œ๊ณผ ๊ฐ™์œผ๋ฉฐ USER_ID, NICKNAME, CITY, STREET_ADDRESS1, STREET_ADDRESS2, TLNO๋Š” ๊ฐ๊ฐ ํšŒ์› ID, ๋‹‰๋„ค์ž„, ์‹œ, ๋„๋กœ๋ช… ์ฃผ์†Œ, ์ƒ์„ธ ์ฃผ์†Œ, ์ „ํ™”๋ฒˆํ˜ธ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

Column name Type Nullable
USER_ID VARCHAR(50) FALSE
NICKNAME VARCHAR(100) FALSE
CITY VARCHAR(100) FALSE
STREET_ADDRESS1 VARCHAR(100) FALSE
STREET_ADDRESS2 VARCHAR(100) TRUE
TLNO VARCHAR(20) FALSE

๋ฌธ์ œ

USED_GOODS_BOARD์™€ USED_GOODS_USER ํ…Œ์ด๋ธ”์—์„œ ์ค‘๊ณ  ๊ฑฐ๋ž˜ ๊ฒŒ์‹œ๋ฌผ์„ 3๊ฑด ์ด์ƒ ๋“ฑ๋กํ•œ ์‚ฌ์šฉ์ž์˜ ์‚ฌ์šฉ์ž ID, ๋‹‰๋„ค์ž„, ์ „์ฒด์ฃผ์†Œ, ์ „ํ™”๋ฒˆํ˜ธ๋ฅผ ์กฐํšŒํ•˜๋Š” SQL๋ฌธ์„ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”. ์ด๋•Œ, ์ „์ฒด ์ฃผ์†Œ๋Š” ์‹œ, ๋„๋กœ๋ช… ์ฃผ์†Œ, ์ƒ์„ธ ์ฃผ์†Œ๊ฐ€ ํ•จ๊ป˜ ์ถœ๋ ฅ๋˜๋„๋ก ํ•ด์ฃผ์‹œ๊ณ , ์ „ํ™”๋ฒˆํ˜ธ์˜ ๊ฒฝ์šฐ xxx-xxxx-xxxx ๊ฐ™์€ ํ˜•ํƒœ๋กœ ํ•˜์ดํ”ˆ ๋ฌธ์ž์—ด(-)์„ ์‚ฝ์ž…ํ•˜์—ฌ ์ถœ๋ ฅํ•ด์ฃผ์„ธ์š”. ๊ฒฐ๊ณผ๋Š” ํšŒ์› ID๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌํ•ด์ฃผ์„ธ์š”.


โ–  ํ’€์ด

  ๋ฌธ์ œ ์š”๊ตฌ์‚ฌํ•ญ  

์ค‘๊ณ  ๊ฑฐ๋ž˜ ๊ฒŒ์‹œ๋ฌผ์„ 3๊ฑด ์ด์ƒ ๋“ฑ๋กํ•œ ์‚ฌ์šฉ์ž์˜ ์‚ฌ์šฉ์ž ID, ๋‹‰๋„ค์ž„, ์ „์ฒด์ฃผ์†Œ, ์ „ํ™”๋ฒˆํ˜ธ๋ฅผ ์กฐํšŒํ•˜๋Š” SQL๋ฌธ์„ ์ž‘์„ฑ

  > BOARD ํ…Œ์ด๋ธ” ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด USER_ID๊ธฐ์ค€์œผ๋กœ ์ง‘๊ณ„ํ•œ ํ›„      - GROUP BY 

  > ์ง‘๊ณ„ ๊ฒฐ๊ณผ์— ๋Œ€ํ•œ ์กฐ๊ฑด (3๊ฑด ์ด์ƒ) ์„ค์ •                                              - HAVING์ ˆ

  > ์‚ฌ์šฉ์ž ์ •๋ณด์— ๋Œ€ํ•œ ์ปฌ๋Ÿผ(๋‹‰๋„ค์ž„, ์ „์ฒด์ฃผ์†Œ, ์ „ํ™”๋ฒˆํ˜ธ)๋ฅผ ์ถœ๋ ฅํ•˜๊ธฐ ์œ„ํ•ด BOARD ํ…Œ์ด๋ธ” ๊ธฐ์ค€์œผ๋กœ left join

 

์ „์ฒด ์ฃผ์†Œ๋Š” ์‹œ, ๋„๋กœ๋ช… ์ฃผ์†Œ, ์ƒ์„ธ ์ฃผ์†Œ๊ฐ€ ํ•จ๊ป˜ ์ถœ๋ ฅ๋˜๋„๋ก ํ•ด์ฃผ์‹œ๊ณ ,

> ๋ฐ์ดํ„ฐํƒ€์ž…์ด ๋ฌธ์ž์ธ ์ปฌ๋Ÿผ๊ฐ’์„ ํ•ฉ์ณ์•ผ ํ•˜๋ฏ€๋กœ CONCAT(str1, str2, str3...)์„ ํ™œ์šฉ

 

์ „ํ™”๋ฒˆํ˜ธ์˜ ๊ฒฝ์šฐ xxx-xxxx-xxxx ๊ฐ™์€ ํ˜•ํƒœ๋กœ ํ•˜์ดํ”ˆ ๋ฌธ์ž์—ด(-)์„ ์‚ฝ์ž…ํ•˜์—ฌ ์ถœ๋ ฅ

> ์ „ํ™”๋ฒˆํ˜ธ ์ปฌ๋Ÿผ(TLNO) ๋˜ํ•œ ๋ฌธ์ž์—ด์ด๋ฏ€๋กœ CONCAT()์„ ์‚ฌ์šฉํ•ด์„œ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹จ ๋ฌธ์ œ ์š”์ฒญ์‚ฌํ•ญ๋Œ€๋กœ '-' ๊นŒ์ง€ ๋”ํ•ด์ค„ ๊ฒƒ 

 

๊ฒฐ๊ณผ๋Š” ํšŒ์› ID๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ  - ์ •๋ ฌ์กฐ๊ฑด

SELECT UGU.USER_ID , UGU.NICKNAME
, CONCAT(UGU.CITY,' ', UGU.STREET_ADDRESS1,' ', UGU.STREET_ADDRESS2) AS ์ „์ฒด์ฃผ์†Œ
, CONCAT(SUBSTR(UGU.TLNO, 1, 3), '-', SUBSTR(UGU.TLNO, 4, 4), '-', SUBSTR(UGU.TLNO, 8, 4)) AS ์ „ํ™”๋ฒˆํ˜ธ
 FROM USED_GOODS_BOARD UGB
LEFT JOIN USED_GOODS_USER UGU
ON UGB.WRITER_ID = UGU.USER_ID

GROUP BY 1
HAVING COUNT(*) >= 3
ORDER BY 1 DESC;

โœ ๋ฌธ์ œํšŒ๊ณ 

SELECT์ ˆ์—์„œ ๋ฌธ์ž์—ด ๊ฐ€๊ณต์ด ๋˜์–ด์„œ ์ถœ๋ ฅ์— ์žˆ์–ด ์ข€๋” ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฌ์ง€ ์•Š์„๊นŒ ์˜ˆ์ƒํ•œ๋‹ค.

ํ˜น์‹œ ์‹œ๊ฐ„์„ ์ค„์ผ ์ˆ˜ ์žˆ๊ฑฐ๋‚˜ ์ข€๋” ํšจ์œจ์ ์ธ ์ฟผ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์‹  ๋ถ„์€ ๋Œ“๊ธ€ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.

๋‹ค๋ฅธ ํ’€์ด ์ฟผ๋ฆฌ๋„ ์ข‹์•„์š” :)