๐Ÿ’ซ ๋ฌธ์ œ



ย 

๐Ÿ’ซ CODE

SELECT a.TITLE, a.BOARD_ID, b.REPLY_ID, b.WRITER_ID, b.CONTENTS, DATE_FORMAT(b.CREATED_DATE, '%Y-%m-%d') AS CREATED_DATE
FROM USED_GOODS_BOARD a 
JOIN USED_GOODS_REPLY b ON a.BOARD_ID = b.BOARD_ID
WHERE YEAR(a.CREATED_DATE) = 2022 AND MONTH(a.CREATED_DATE) = 10
ORDER BY b.CREATED_DATE asc, a.TITLE asc;

ย 

๐Ÿ’ซ ํ’€์ด ์กฐ๊ฑด

1. 2022๋…„ 10์›”์— ์ž‘์„ฑ๋œ ๊ฒŒ์‹œ๊ธ€
2. ๋Œ“๊ธ€ ์ž‘์„ฑ์ผ์„ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ
3. ๋Œ“๊ธ€ ์ž‘์„ฑ์ผ์ด ๊ฐ™๋‹ค๋ฉด ๊ฒŒ์‹œ๊ธ€ ์ œ๋ชฉ์„ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ

ย 

๐Ÿ’ซ ํ’€์ด

1. SELECT a.TITLE, a.BOARD_ID, b.REPLY_ID, b.WRITER_ID, b.CONTENTS, DATE_FORMAT(b.CREATED_DATE, '%Y-%m-%d') AS CREATED_DATE
  • USED_GOODS_BOARD ํ…Œ์ด๋ธ”์˜ ๊ฒŒ์‹œ๊ธ€ ์ œ๋ชฉ (TITLE), ๊ฒŒ์‹œ๊ธ€ ID (BOARD_ID) ์กฐํšŒ
  • USED_GOODS_REPLY ํ…Œ์ด๋ธ”์˜ ๋Œ“๊ธ€ ID (REPLY_ID), ์ž‘์„ฑ์ž ID (WRITER_ID), ๋Œ“๊ธ€ ๋‚ด์šฉ(CONTENTS), ๋Œ“๊ธ€ ์ž‘์„ฑ์ผ (CREATED_DATE) ์กฐํšŒ
2. DATE_FORMAT(b.CREATED_DATE, '%Y-%m-%d') AS CREATED_DATE
  • DATE_FORMAT ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ž‘์„ฑ์ผ์ด ๋…„๋„-์›”-์ผ ๊นŒ์ง€๋งŒ ์ถœ๋ ฅ ๋˜๋„๋ก ๋ณ€๊ฒฝ
  • AS๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ปฌ๋Ÿผ๋ช…์— CREATED_DATE๋กœ ๋ณ„์นญ ์ฃผ๊ธฐ
3. FROM USED_GOODS_BOARD a
  • USED_GOODS_BOARD ํ…Œ์ด๋ธ” ๋ณ„์นญ์„ a๋กœ ์ง€์ •
4. JOIN USED_GOODS_REPLY b ON a.BOARD_ID = b.BOARD_ID
  • JOIN ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‹ค๋ฅธ ํ…Œ์ด๋ธ”(USED_GOODS_REPLY) ์กฐ์ธ
  • ์กฐ์ธ ์กฐ๊ฑด : ON a.BOARD_ID = b.BOARD_ID
  • USED_GOODS_REPLY ํ…Œ์ด๋ธ” ๋ณ„์นญ b๋กœ ์ง€์ •
5. WHERE YEAR(a.CREATED_DATE) = 2022 AND MONTH(a.CREATED_DATE) = 10
  • ์กฐ๊ฑด : ๊ธ€ ์ž‘์„ฑ์ผ(a.CREATED_DATE) ์ค‘ ๋…„๋„๊ฐ€ 2022๋…„๋„ ์ด๊ณ , ์›”์ด 10์›”์ธ ์ปฌ๋Ÿผ
6. ORDER BY b.CREATED_DATE asc, a.TITLE asc;
  • ORDER BY๋ฅผ ์ด์šฉํ•˜์—ฌ ๋Œ“๊ธ€ ์ž‘์„ฑ์ผ(b.CREATED_DATE) ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ, ๋Œ“๊ธ€ ์ž‘์„ฑ์ผ์ด ๊ฐ™๋‹ค๋ฉด ๊ฒŒ์‹œ๊ธ€ ์ œ๋ชฉ(TITLE)์„ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ

By Dozzing

๋‹ต๊ธ€ ๋‚จ๊ธฐ๊ธฐ

์ด๋ฉ”์ผ ์ฃผ์†Œ๋Š” ๊ณต๊ฐœ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํ•„์ˆ˜ ํ•„๋“œ๋Š” *๋กœ ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค