Growth ๐ŸŒณ/Practice ๐Ÿ’ป

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์กฐ๊ฑด์— ๋ถ€ํ•ฉํ•˜๋Š” ์ค‘๊ณ ๊ฑฐ๋ž˜ ๋Œ“๊ธ€ ์กฐํšŒํ•˜๊ธฐ

์ธ” 2023. 3. 19. 14:29

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

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

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

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


โ–  ๋ฌธ์ œ

๋ฌธ์ œ ์„ค๋ช…

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

 USED_GOODS_BOARD ํ…Œ์ด๋ธ”์€ ๋‹ค์Œ๊ณผ ๊ฐ™์œผ๋ฉฐ BOARD_IDWRITER_IDTITLECONTENTSPRICECREATED_DATESTATUSVIEWS์€ ๊ฒŒ์‹œ๊ธ€ 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_REPLY ํ…Œ์ด๋ธ”์€ ๋‹ค์Œ๊ณผ ๊ฐ™์œผ๋ฉฐ REPLY_IDBOARD_IDWRITER_IDCONTENTSCREATED_DATE๋Š” ๊ฐ๊ฐ ๋Œ“๊ธ€ ID, ๊ฒŒ์‹œ๊ธ€ ID, ์ž‘์„ฑ์ž ID, ๋Œ“๊ธ€ ๋‚ด์šฉ, ์ž‘์„ฑ์ผ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค

Column name Type Nullable
REPLY_ID VARCHAR(10) FALSE
BOARD_ID VARCHAR(5) FALSE
WRITER_ID VARCHAR(50) FALSE
CONTENTS VARCHAR(1000) TRUE
CREATED_DATE DATE FALSE

๋ฌธ์ œ

USED_GOODS_BOARD์™€ USED_GOODS_REPLY ํ…Œ์ด๋ธ”์—์„œ 2022๋…„ 10์›”์— ์ž‘์„ฑ๋œ ๊ฒŒ์‹œ๊ธ€ ์ œ๋ชฉ, ๊ฒŒ์‹œ๊ธ€ ID, ๋Œ“๊ธ€ ID, ๋Œ“๊ธ€ ์ž‘์„ฑ์ž ID, ๋Œ“๊ธ€ ๋‚ด์šฉ, ๋Œ“๊ธ€ ์ž‘์„ฑ์ผ์„ ์กฐํšŒํ•˜๋Š” SQL๋ฌธ์„ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”. ๊ฒฐ๊ณผ๋Š” ๋Œ“๊ธ€ ์ž‘์„ฑ์ผ์„ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•ด์ฃผ์‹œ๊ณ , ๋Œ“๊ธ€ ์ž‘์„ฑ์ผ์ด ๊ฐ™๋‹ค๋ฉด ๊ฒŒ์‹œ๊ธ€ ์ œ๋ชฉ์„ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•ด์ฃผ์„ธ์š”.


โ–  ํ’€์ด

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

2022๋…„ 10์›”์— ์ž‘์„ฑ๋œ ๊ฒŒ์‹œ๊ธ€ ์ œ๋ชฉ, ๊ฒŒ์‹œ๊ธ€ ID, ๋Œ“๊ธ€ ID, ๋Œ“๊ธ€ ์ž‘์„ฑ์ž ID, ๋Œ“๊ธ€ ๋‚ด์šฉ, ๋Œ“๊ธ€ ์ž‘์„ฑ์ผ์„ ์กฐํšŒ

> ์ž‘์„ฑ์›”์ด 2022.10์ธ ๊ฒŒ์‹œ๊ธ€ - WHERE ์กฐ๊ฑด1

๊ฒฐ๊ณผ๋Š” ๋Œ“๊ธ€ ์ž‘์„ฑ์ผ์„ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ                            < ์ •๋ ฌ์กฐ๊ฑด1

๋Œ“๊ธ€ ์ž‘์„ฑ์ผ์ด ๊ฐ™๋‹ค๋ฉด ๊ฒŒ์‹œ๊ธ€ ์ œ๋ชฉ์„ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ < ์ •๋ ฌ์กฐ๊ฑด2

 

ํ…Œ์ด๋ธ” ์กฐ์ธํ•  ๋•Œ (๊ฒŒ์‹œ๊ธ€:๋Œ“๊ธ€) = 1 : N ๊ด€๊ณ„๋ผ๋Š” ๊ฒƒ์„ ์ƒ๊ฐํ•˜๊ณ  JOIN์„ ํ•ด์ค˜์•ผ ํ•œ๋‹ค.

๊ทธ๋ž˜์„œ ๋Œ“๊ธ€ ํ…Œ์ด๋ธ” ๊ธฐ์ค€์œผ๋กœ left join์„ ํ•ด์คฌ๋‹ค.

WHERE์ ˆ์€ ๋ฐ์ดํ„ฐ ๊ฐ€๊ณต์„ ์ค„์ด๊ธฐ ์œ„ํ•ด BETWEEN์ ˆ์„ ์‚ฌ์šฉํ–ˆ๋‹ค.

SELECT A.TITLE AS TITLE
    , A.BOARD_ID AS BOARD_ID
    , B.REPLY_ID AS REPLY_ID
    , B.WRITER_ID AS WRITER_ID
    , B.CONTENTS AS CONTENTS
    , DATE_FORMAT(B.CREATED_DATE, '%Y-%m-%d') AS CREATED_DATE
 FROM USED_GOODS_REPLY B
 LEFT JOIN USED_GOODS_BOARD A
 ON A.BOARD_ID = B.BOARD_ID
 WHERE A.CREATED_DATE BETWEEN '2022-10-01 00:00:00' AND '2022-10-31 23:59:59'
 ORDER BY 6, 1;

โœ ํ’€์ด ํšŒ๊ณ 

์ฒ˜์Œ์— ๋ฌด์ž‘์ • ๋Œ“๊ธ€์ด ์—†๋Š” ๊ฒŒ์‹œ๋ฌผ์ด ์žˆ์„์ˆ˜๋„ ์žˆ๋‹ค๋Š” ์ƒ๊ฐ์— ๊ฒŒ์‹œ๊ธ€ ํ…Œ์ด๋ธ” ๊ธฐ์ค€์œผ๋กœ left join์„ ํ•ด์ค€ ๊ฒƒ์ด ์˜ค๋ฅ˜์˜€๋‹ค.

๋ฌธ์ œ ์ž˜ ์ฝ๊ณ  ํ…Œ์ด๋ธ” ๊ฐ„ ๊ด€๊ณ„์— ๋Œ€ํ•ด ๊ผผ๊ผผํ•˜๊ฒŒ ์ƒ๊ฐํ•ด๋ณผ ๊ฒƒ.

 ๋˜ํ•œ ์กฐ์ธํ•  ๋•Œ ํ…Œ์ด๋ธ”๋ณ„ ์ปฌ๋Ÿผ๋ช…์ด ๊ฐ™๋‹ค๊ณ  ํ•ด์„œ ๊ณตํ†ต ์ปฌ๋Ÿผ์ด๋ผ๊ณ  ์„ฃ๋ถˆ๋ฆฌ ์ƒ๊ฐํ•˜์ง€๋ง ๊ฒƒ.

 ์˜ˆ) WRITER_ID