diff --git "a/keyword/chapter02/images/\354\213\244\354\212\2651.png" "b/keyword/chapter02/images/\354\213\244\354\212\2651.png" new file mode 100644 index 0000000..5f122c9 Binary files /dev/null and "b/keyword/chapter02/images/\354\213\244\354\212\2651.png" differ diff --git "a/keyword/chapter02/images/\354\213\244\354\212\26510.png" "b/keyword/chapter02/images/\354\213\244\354\212\26510.png" new file mode 100644 index 0000000..203a924 Binary files /dev/null and "b/keyword/chapter02/images/\354\213\244\354\212\26510.png" differ diff --git "a/keyword/chapter02/images/\354\213\244\354\212\2652.png" "b/keyword/chapter02/images/\354\213\244\354\212\2652.png" new file mode 100644 index 0000000..88d2722 Binary files /dev/null and "b/keyword/chapter02/images/\354\213\244\354\212\2652.png" differ diff --git "a/keyword/chapter02/images/\354\213\244\354\212\2653.png" "b/keyword/chapter02/images/\354\213\244\354\212\2653.png" new file mode 100644 index 0000000..cea1a68 Binary files /dev/null and "b/keyword/chapter02/images/\354\213\244\354\212\2653.png" differ diff --git "a/keyword/chapter02/images/\354\213\244\354\212\2654.png" "b/keyword/chapter02/images/\354\213\244\354\212\2654.png" new file mode 100644 index 0000000..ca9af55 Binary files /dev/null and "b/keyword/chapter02/images/\354\213\244\354\212\2654.png" differ diff --git "a/keyword/chapter02/images/\354\213\244\354\212\2655.png" "b/keyword/chapter02/images/\354\213\244\354\212\2655.png" new file mode 100644 index 0000000..df00b58 Binary files /dev/null and "b/keyword/chapter02/images/\354\213\244\354\212\2655.png" differ diff --git "a/keyword/chapter02/images/\354\213\244\354\212\2656.png" "b/keyword/chapter02/images/\354\213\244\354\212\2656.png" new file mode 100644 index 0000000..7b0a442 Binary files /dev/null and "b/keyword/chapter02/images/\354\213\244\354\212\2656.png" differ diff --git "a/keyword/chapter02/images/\354\213\244\354\212\2657.png" "b/keyword/chapter02/images/\354\213\244\354\212\2657.png" new file mode 100644 index 0000000..111cbb5 Binary files /dev/null and "b/keyword/chapter02/images/\354\213\244\354\212\2657.png" differ diff --git "a/keyword/chapter02/images/\354\213\244\354\212\2658.png" "b/keyword/chapter02/images/\354\213\244\354\212\2658.png" new file mode 100644 index 0000000..cbdf392 Binary files /dev/null and "b/keyword/chapter02/images/\354\213\244\354\212\2658.png" differ diff --git "a/keyword/chapter02/images/\354\213\244\354\212\2659.png" "b/keyword/chapter02/images/\354\213\244\354\212\2659.png" new file mode 100644 index 0000000..0dcf22a Binary files /dev/null and "b/keyword/chapter02/images/\354\213\244\354\212\2659.png" differ diff --git a/keyword/chapter02/keyword.md b/keyword/chapter02/keyword.md new file mode 100644 index 0000000..07c0415 --- /dev/null +++ b/keyword/chapter02/keyword.md @@ -0,0 +1,113 @@ +### **๐Ÿ“ฆ ์‹ค์Šต** +--- +(์ƒ์„ฑํ•œ ํ…Œ์ด๋ธ”์— ์ž„์˜์˜ ๋ฐ์ดํ„ฐ๋“ค์„ ์‚ฝ์ž…ํ•˜์—ฌ ํ…Œ์ŠคํŠธํ•ด๋ณด์•˜๋‹ค.) + +**๊ฐ ์ฑ…์˜ ์ข‹์•„์š” ๊ฐœ์ˆ˜๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ์ฟผ๋ฆฌ** +```sql +select book_id, count(*) from likes group by book_id +``` +![์‹ค์Šต1](images/์‹ค์Šต1.png) + +**๋‚ด๊ฐ€ ์ฐจ๋‹จํ•œ ์‚ฌ์šฉ์ž์˜ ์ข‹์•„์š”๋Š” ์ง‘๊ณ„ํ•˜์ง€ ์•Š์€, ์ฑ…์˜ ์ข‹์•„์š” ๊ฐœ์ˆ˜๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ์ฟผ๋ฆฌ** +1. ์„œ๋ธŒ์ฟผ๋ฆฌ ์‚ฌ์šฉ + ```sql + select count(*) as book_likes from likes where book_id = 3 + and member_id not in (select blocked_id from block where blocker_id = 2) + ``` + ![์‹ค์Šต2](images/์‹ค์Šต2.png) +2. left join ์‚ฌ์šฉ + ```sql + select count(*) as book_likes from likes as l + left join block as b on l.member_id = b.blocked_id and b.blocker_id = 2 + where b.blocked_id is null and l.book_id = 3 + ``` + ![์‹ค์Šต3](images/์‹ค์Šต3.png) +- id๊ฐ€ 2์ธ ์‚ฌ์šฉ์ž๊ฐ€ id๊ฐ€ 3์ธ ์ฑ…์— ๋Œ€ํ•ด ์ž์‹ ์ด ์ฐจ๋‹จํ•œ ์‚ฌ์šฉ์ž๊ฐ€ ๋ˆ„๋ฅธ ์ข‹์•„์š”๋ฅผ ์ œ์™ธํ•œ ์ข‹์•„์š” ๊ฐœ์ˆ˜๋ฅผ ์กฐํšŒํ•œ๋‹ค. + +**ํ•ด์‹œํƒœ๊ทธ๋ฅผ ํ†ตํ•ด ์ฑ…์„ ๊ฒ€์ƒ‰ํ•˜๋Š” ์ฟผ๋ฆฌ** +1. ์„œ๋ธŒ์ฟผ๋ฆฌ ์‚ฌ์šฉ + ```sql + select * from book where id in + (select book_id from book_hash_tag + where hash_tag_id = (select id from hash_tag where title = "Hashtag5")); + ``` + ![์‹ค์Šต4](images/์‹ค์Šต4.png) + +2. inner join 2๋ฒˆ ์‚ฌ์šฉ + ```sql + select b.* from book as b + inner join book_hash_tag as bht on bht.book_id = b.id + inner join hash_tag as ht on ht.id = bht.hash_tag_id + where ht.title = "Hashtag5" + ``` + ![์‹ค์Šต5](images/์‹ค์Šต5.png) + +- Hashtag5๋ผ๋Š” ํƒœ๊ทธ์˜ ์ฑ…์„ ์กฐํšŒํ•œ๋‹ค. + +**์ข‹์•„์š” ๊ฐœ์ˆ˜ ์ˆœ(์ธ๊ธฐ์ˆœ)์œผ๋กœ ์ฑ…์„ ์กฐํšŒํ•˜๋Š” ์ฟผ๋ฆฌ** +```sql +select * from book as b +join (select book_id, count(*) as book_likes from likes group by book_id) as bl + on b.id = bl.book_id +order by bl.book_likes desc +``` +![์‹ค์Šต6](images/์‹ค์Šต6.png) + +**์ข‹์•„์š” ๊ฐœ์ˆ˜ ์ˆœ(์ธ๊ธฐ์ˆœ)์œผ๋กœ ์ฑ…์„ ํŽ˜์ด์ง•ํ•˜์—ฌ ์กฐํšŒํ•˜๋Š” ์ฟผ๋ฆฌ** +```sql +select * from book as b +join (select book_id, count(*) as book_likes from likes group by book_id) as bl + on b.id = bl.book_id +order by bl.book_likes desc +limit 5 offset 10 +``` +![์‹ค์Šต7](images/์‹ค์Šต7.png) +- ๋ฐ์ดํ„ฐ 10๊ฐœ๋ฅผ ๊ฑด๋„ˆ๋›ด 4๊ฐœ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด์—ฌ์ค€๋‹ค. (10 = 5 * 2 โ†’ ํŽ˜์ด์ง€ 2๋ฒˆ์— ๋Œ€ํ•ด ํ•œ ํŽ˜์ด์ง€์— 5๊ฐœ๋ฅผ ๋ณด์—ฌ์ค€๋‹ค) + +**์ข‹์•„์š” ๊ฐœ์ˆ˜ ์ˆœ์œผ๋กœ ๋‚˜์—ด๋œ ์ฑ… ์ค‘ ๋งˆ์ง€๋ง‰์œผ๋กœ ์กฐํšŒํ•œ ์ฑ… ๋‹ค์Œ์˜ ์ฑ…๋“ค์„ ์กฐํšŒํ•˜๋Š” ์ฟผ๋ฆฌ** +```sql +select * from book as b +join (select book_id, count(*) as book_likes from likes group by book_id) as bl + on b.id = bl.book_id +where bl.book_likes < (select count(*) from likes where book_id = 2) +order by bl.book_likes desc, b.id desc limit 7 +``` +![์‹ค์Šต8](images/์‹ค์Šต8.png) +- ๋งˆ์ง€๋ง‰์œผ๋กœ ์กฐํšŒํ•œ, id๊ฐ€ 2์ธ ์ฑ… ๋‹ค์Œ์˜ ์ฑ…(์ข‹์•„์š” ๊ฐœ์ˆ˜๊ฐ€ ๋” ์ ์€ ์ฑ…)๋“ค์„ 7๊ฐœ ์กฐํšŒํ•œ๋‹ค. + +**cursor ๊ฐ’์„ ์ข‹์•„์š” ๊ฐœ์ˆ˜์™€ ์ฑ… id๋กœ ์—ฎ์€ ๊ณ ์œ ํ•œ ๊ฐ’์œผ๋กœ ๋งŒ๋“ค์–ด ์กฐํšŒํ•˜๋Š” ์ฟผ๋ฆฌ** +1. having ์‚ฌ์šฉ + ```sql + select b.*, concat(lpad(l.book_likes, 10, '0'), lpad(b.id, 10, '0')) as cursor_value + from book as b + join (select book_id, count(*) as book_likes from likes group by book_id) as l on l.book_id = b.id + having cursor_value < (select concat(lpad(l_sub.book_likes, 10, '0'), lpad(l_sub.book_id, 10, '0')) + from (select book_id, count(*) as book_likes from likes group by book_id) as l_sub + where l_sub.book_id = 3) + order by l.book_likes desc, b.id desc limit 7 + ``` + ![์‹ค์Šต9](images/์‹ค์Šต9.png) +2. where ์‚ฌ์šฉ + ```sql + select b.*, concat(lpad(l.book_likes, 10, '0'), lpad(b.id, 10, '0')) as cursor_value + from book as b + join (select book_id, count(*) as book_likes from likes group by book_id) as l on l.book_id = b.id + where concat(lpad(l.book_likes, 10, '0'), lpad(b.id, 10, '0')) + < ( select concat(lpad(l_sub.book_likes, 10, '0'), lpad(l_sub.book_id, 10, '0')) + from (select book_id, count(*) as book_likes from likes group by book_id) as l_sub + where l_sub.book_id = 3) + order by l.book_likes desc, b.id desc limit 7 + ``` + ![์‹ค์Šต10](images/์‹ค์Šต10.png) +- id๊ฐ€ 3์ธ ์ฑ…๋ณด๋‹ค ์ข‹์•„์š” ๊ฐœ์ˆ˜๊ฐ€ ๋” ์ ๊ณ  id ๊ฐ’์ด ๋” ์ž‘์€ ์ฑ…๋“ค์„ 7๊ฐœ ์กฐํšŒํ•œ๋‹ค. +- LPAD(๋ฌธ์ž์—ด, ์ „์ฒด ๊ธธ์ด, ์ฑ„์šธ ๋ฌธ์ž) + - ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ ์ผ์ •ํ•˜๊ฒŒ ๋งž์ถ”๊ธฐ ์œ„ํ•ด, ๋ถ€์กฑํ•œ ๋ถ€๋ถ„์„ ํŠน์ • ๋ฌธ์ž๋กœ ์ฑ„์›Œ์ฃผ๋Š” ํ•จ์ˆ˜ + - likes.like_count ๊ฐ’์„ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•œ ํ›„, ๊ธธ์ด๋ฅผ 10์ž๋ฆฌ๋กœ ๋งž์ถ”๊ณ , ๋นˆ ์ž๋ฆฌ๋Š” '0'์œผ๋กœ ์ฑ„์šด๋‹ค. + - ex. like_count = 5์ผ ๋•Œ, LPAD(like_count, 10, '0') โ†’ '0000000005' +- CONCAT(๋ฌธ์ž์—ด 1, ๋ฌธ์ž์—ด 2) + - 2๊ฐœ ์ด์ƒ์˜ ๋ฌธ์ž์—ด์„ ๊ฒฐํ•ฉํ•˜๋Š” ํ•จ์ˆ˜ + - ex. like_count = 5, [b.id](http://b.id) = 12์ผ ๋•Œ + CONCAT('0000000005', '0000000012') โ†’ '00000000050000000012' +- HAVING vs. WHERE + - WHERE: ๊ทธ๋ฃนํ™” ์ด์ „์— ๊ฐ ํ–‰์— ๋Œ€ํ•ด ์กฐ๊ฑด์„ ์ ์šฉํ•œ๋‹ค. + - HAVING: ๊ทธ๋ฃนํ™” ์ดํ›„์— ๊ทธ๋ฃนํ™”๋œ ๊ฒฐ๊ณผ์— ๋Œ€ํ•ด ์กฐ๊ฑด์„ ์ ์šฉํ•œ๋‹ค. \ No newline at end of file diff --git "a/mission/chapter02/images/\352\260\200\352\262\214-\353\260\251\353\254\270-\353\257\270\354\205\230-\355\217\254\354\235\270\355\212\270-\354\204\234\353\271\204\354\212\244-\354\212\244\355\204\260\353\224\224-\355\233\204-\354\266\224\352\260\200\354\210\230\354\240\225.jpg" "b/mission/chapter02/images/\352\260\200\352\262\214-\353\260\251\353\254\270-\353\257\270\354\205\230-\355\217\254\354\235\270\355\212\270-\354\204\234\353\271\204\354\212\244-\354\212\244\355\204\260\353\224\224-\355\233\204-\354\266\224\352\260\200\354\210\230\354\240\225.jpg" new file mode 100644 index 0000000..98712e8 Binary files /dev/null and "b/mission/chapter02/images/\352\260\200\352\262\214-\353\260\251\353\254\270-\353\257\270\354\205\230-\355\217\254\354\235\270\355\212\270-\354\204\234\353\271\204\354\212\244-\354\212\244\355\204\260\353\224\224-\355\233\204-\354\266\224\352\260\200\354\210\230\354\240\225.jpg" differ diff --git "a/mission/chapter02/images/\353\257\270\354\205\2301-1-\354\277\274\353\246\254\354\236\221\354\204\261.png" "b/mission/chapter02/images/\353\257\270\354\205\2301-1-\354\277\274\353\246\254\354\236\221\354\204\261.png" new file mode 100644 index 0000000..d776628 Binary files /dev/null and "b/mission/chapter02/images/\353\257\270\354\205\2301-1-\354\277\274\353\246\254\354\236\221\354\204\261.png" differ diff --git "a/mission/chapter02/images/\353\257\270\354\205\2301-2-\354\277\274\353\246\254\354\236\221\354\204\261.png" "b/mission/chapter02/images/\353\257\270\354\205\2301-2-\354\277\274\353\246\254\354\236\221\354\204\261.png" new file mode 100644 index 0000000..ca81de0 Binary files /dev/null and "b/mission/chapter02/images/\353\257\270\354\205\2301-2-\354\277\274\353\246\254\354\236\221\354\204\261.png" differ diff --git "a/mission/chapter02/images/\353\257\270\354\205\2302-1-\354\277\274\353\246\254\354\236\221\354\204\261.png" "b/mission/chapter02/images/\353\257\270\354\205\2302-1-\354\277\274\353\246\254\354\236\221\354\204\261.png" new file mode 100644 index 0000000..c8ad587 Binary files /dev/null and "b/mission/chapter02/images/\353\257\270\354\205\2302-1-\354\277\274\353\246\254\354\236\221\354\204\261.png" differ diff --git "a/mission/chapter02/images/\353\257\270\354\205\2302-2-\354\277\274\353\246\254\354\236\221\354\204\261.png" "b/mission/chapter02/images/\353\257\270\354\205\2302-2-\354\277\274\353\246\254\354\236\221\354\204\261.png" new file mode 100644 index 0000000..04297a7 Binary files /dev/null and "b/mission/chapter02/images/\353\257\270\354\205\2302-2-\354\277\274\353\246\254\354\236\221\354\204\261.png" differ diff --git "a/mission/chapter02/images/\353\257\270\354\205\2303-1-\354\277\274\353\246\254\354\236\221\354\204\261.png" "b/mission/chapter02/images/\353\257\270\354\205\2303-1-\354\277\274\353\246\254\354\236\221\354\204\261.png" new file mode 100644 index 0000000..a11f348 Binary files /dev/null and "b/mission/chapter02/images/\353\257\270\354\205\2303-1-\354\277\274\353\246\254\354\236\221\354\204\261.png" differ diff --git "a/mission/chapter02/images/\353\257\270\354\205\2303-2-\354\277\274\353\246\254\354\236\221\354\204\261.png" "b/mission/chapter02/images/\353\257\270\354\205\2303-2-\354\277\274\353\246\254\354\236\221\354\204\261.png" new file mode 100644 index 0000000..51ec4ee Binary files /dev/null and "b/mission/chapter02/images/\353\257\270\354\205\2303-2-\354\277\274\353\246\254\354\236\221\354\204\261.png" differ diff --git "a/mission/chapter02/images/\353\257\270\354\205\2304-\354\277\274\353\246\254\354\236\221\354\204\261.png" "b/mission/chapter02/images/\353\257\270\354\205\2304-\354\277\274\353\246\254\354\236\221\354\204\261.png" new file mode 100644 index 0000000..1e3ffdb Binary files /dev/null and "b/mission/chapter02/images/\353\257\270\354\205\2304-\354\277\274\353\246\254\354\236\221\354\204\261.png" differ diff --git a/mission/chapter02/mission.md b/mission/chapter02/mission.md new file mode 100644 index 0000000..822c460 --- /dev/null +++ b/mission/chapter02/mission.md @@ -0,0 +1,393 @@ +### **๐Ÿ”ฅ ๋ฏธ์…˜** +--- +#### **์Šคํ„ฐ๋”” ์ „์˜ ์ฟผ๋ฆฌ ์ž‘์„ฑ** + +์ž„์˜์˜ ๊ฐ’์„ ๋„ฃ์–ด์„œ ์ฟผ๋ฆฌ๋ฅผ ์ž‘์„ฑํ•˜์˜€๋‹ค. + +**๋‚ด๊ฐ€ ์ง„ํ–‰ ์ค‘, ์ง„ํ–‰ ์™„๋ฃŒํ•œ ๋ฏธ์…˜์„ ๋ชจ์•„์„œ ๋ณด๋Š” ์ฟผ๋ฆฌ (ํŽ˜์ด์ง• ํฌํ•จ)** +1. ์ง„ํ–‰ ์ค‘์ธ ๋ฏธ์…˜ ๋ชฉ๋ก + ```sql + select member_name, restaurant_name, rm.points, introduction from member as m + inner join member_mission as mm on m.id = mm.member_id + inner join (select m.*, restaurant_name from mission as m + join (select restaurant.* from restaurant) as r + on m.restaurant_id = r.id where ongoing = 1) as rm + on rm.id = mm.mission_id + where m.id = 1 + order by m.created_at limit 5 offset 0; + + ``` +2. ์ง„ํ–‰ ์™„๋ฃŒํ•œ ๋ฏธ์…˜ ๋ชฉ๋ก + ```sql + select member_name, restaurant_name, rm.points, introduction from member as m + inner join member_mission as mm on m.id = mm.member_id + inner join (select restaurant_name, m.* from mission as m + join (select restaurant.* from restaurant) as r + on m.restaurant_id = r.id where is_completed = 1) as rm + on rm.id = mm.mission_id + where m.id = 1 + order by m.created_at limit 5 offset 0; + + ``` +- id๊ฐ€ 1์ธ ์‚ฌ์šฉ์ž์˜ ์ง„ํ–‰ ์ค‘ ๋ฐ ์ง„ํ–‰ ์™„๋ฃŒํ•œ ๋ฏธ์…˜๋“ค์„ ์กฐํšŒํ•œ๋‹ค. + +**๋ฆฌ๋ทฐ ์ž‘์„ฑํ•˜๋Š” ์ฟผ๋ฆฌ** +```sql +select nickname, restaurant_name, rating, content, image_url, rm.created_at, reply_content from restaurant as rest +join (select m.nickname, ri.* from member as m + join (select i.image_url, i.created_at as image_created_at, rr.* from image as i + right join (select re.writer, re.created_at as reply_created_at, r.*, re.content as reply_content from review as r + left join reply as re + on re.review_id = r.id) as rr + on i.review_id = rr.id) as ri + on m.id = ri.member_id) as rm +on rest.id = rm.restaurant_id and rest.id = 1 +order by created_at desc, reply_created_at desc, image_created_at desc limit 5 offset 0; + +``` +- id๊ฐ€ 1์ธ ์‹๋‹น์˜ ๋ฆฌ๋ทฐ๋ฅผ ๋ชจ์•„๋ณธ๋‹ค. + +**ํ™ˆ ํ™”๋ฉด ์ฟผ๋ฆฌ (ํ˜„์žฌ ์„ ํƒ ๋œ ์ง€์—ญ์—์„œ ๋„์ „์ด ๊ฐ€๋Šฅํ•œ ๋ฏธ์…˜ ๋ชฉ๋ก, ํŽ˜์ด์ง• ํฌํ•จ)** +1. ํ˜„์žฌ ์„ ํƒ๋œ ์ง€์—ญ(์‚ฌ์šฉ์ž ๊ฑฐ์ฃผ์ง€) ๋ฐ ์ง„ํ–‰ ์™„๋ฃŒํ•œ ๋ฏธ์…˜์˜ ๊ฐœ์ˆ˜ + ```sql + select location, count(*) as mission_completed from + (select m.id, ma.map_location as location from member as m + join map as ma on ma.id = m.residence_id) as mma + inner join member_mission as mm on mma.id = mm.member_id + inner join mission as mi on mm.mission_id = mi.id + where mi.is_completed = 1 and mma.id = 4; + ``` + - ๊ทธ๋Ÿฌ๋‚˜, ์ง„ํ–‰ ์™„๋ฃŒํ•œ ๋ฏธ์…˜์˜ ๊ฐœ์ˆ˜๊ฐ€ 0์ผ ๊ฒฝ์šฐ์—” location ๊ฐ’์ด NULL์ด ๋‚˜์˜ค๋Š” ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค. inner join์€ ๋‘ ํ…Œ์ด๋ธ” ๊ฐ„์— ์ผ์น˜ํ•˜๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ์„ ๋•Œ๋งŒ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฏ€๋กœ, โ€œmi.is_completed = 1โ€ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๋ฏธ์…˜์ด ์—†์œผ๋ฉด ํ•ด๋‹น member์™€ ๊ด€๋ จ๋œ ๋ฐ์ดํ„ฐ๊ฐ€ ๊ฒฐ๊ณผ์—์„œ ์ œ์™ธ๋˜์–ด ์™„๋ฃŒ๋œ ๋ฏธ์…˜์ด ์—†๋Š” ๊ฒฝ์šฐ location๋„ ๊ฒฐ๊ณผ์— ํฌํ•จ๋˜์ง€ ์•Š๊ณ  NULL์ด ๋˜๋Š” ๊ฒƒ์œผ๋กœ ๋ณด์˜€๋‹ค. ์ง„ํ–‰ ์™„๋ฃŒํ•œ ๋ฏธ์…˜์ด ์—†๋”๋ผ๋„ location์€ NULL์ด ์•„๋‹Œ ๊ฐ’์ด ๋‚˜์˜ค๋„๋ก ์ˆ˜์ •ํ•ด์•ผ ํ•œ๋‹ค. +2. ํ˜„์žฌ ์„ ํƒ ๋œ ์ง€์—ญ์—์„œ ๋„์ „์ด ๊ฐ€๋Šฅํ•œ ๋ฏธ์…˜ ๋ชฉ๋ก + ```sql + select restaurant_name, kind, introduction, frm.points, frm.dday from member as mem + inner join member_mission as mm on mm.mission_id = mem.id + inner join (select fr.kind, fr.restaurant_name, m.* from mission as m + join (select r.id, r.restaurant_name, fk.kind from food_kind as fk + inner join food_kind_restaurant as fkr on fkr.food_kind_id = fk.id + inner join restaurant as r on fkr.restaurant_id = r.id) as fr on fr.id = m.restaurant_id + where is_completed = 0 and ongoing = 0) as frm on frm.id = mm.mission_id + where mem.id = 4 + order by frm.created_at limit 5 offset 0; + ``` +- id๊ฐ€ 4์ธ ์‚ฌ์šฉ์ž์˜ ๊ฑฐ์ฃผ์ง€์™€ ์ง„ํ–‰ ์™„๋ฃŒํ•œ ๋ฏธ์…˜์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ฐ€์ ธ์˜ค๊ณ , ๋„์ „์ด ๊ฐ€๋Šฅํ•œ ๋ฏธ์…˜๋“ค์„ ์กฐํšŒํ•œ๋‹ค. + +**๋งˆ์ด ํŽ˜์ด์ง€ ํ™”๋ฉด ์ฟผ๋ฆฌ** +```sql +select nickname, email, phone_number, points +from member +where id = 1 +``` +- id๊ฐ€ 1์ธ ์‚ฌ์šฉ์ž์˜ ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค. + +#### **์Šคํ„ฐ๋”” ํ›„์˜ ์ฟผ๋ฆฌ ์ž‘์„ฑ** + +**์Šคํ„ฐ๋””์—์„œ ๋ฐฐ์šด ๊ฒƒ + ๊ณ ์น  ์ ** +1. ์ฟผ๋ฆฌ๋ฅผ ์ž‘์„ฑํ•˜๊ธฐ์— ์•ž์„œ์„œ ์ถ”์ถœํ•˜๋ ค๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋”ฐ์ ธ๋ณธ๋‹ค. ์ถ”์ถœํ•˜๋ ค๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ์–ด๋–ค ํ…Œ์ด๋ธ”์„ ์‚ฌ์šฉํ•  ๊ฒƒ์ธ์ง€ ์ถ”์ธกํ•œ๋‹ค. + - ex. + ```sql + select member_name, restaurant_name, review_content + ``` + - ์ด ๊ฒฝ์šฐ member, restaurant, reveiew ํ…Œ์ด๋ธ”๋“ค์„ join ํ•ด์•ผ๊ฒ ๊ตฌ๋‚˜ ~ ์ƒ๊ฐํ•ด๋ณธ๋‹ค. +2. ํšจ์œจ์ ์ธ ๋ฐ์ดํ„ฐ ์ƒํƒœ ๊ด€๋ฆฌ๋ฅผ ์œ„ํ•ด ๊ฐ ํ…Œ์ด๋ธ”๋งˆ๋‹ค ์†์„ฑ์œผ๋กœ status๋ฅผ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ๊ฐ ํ…Œ์ด๋ธ”์— ๋”ฐ๋ผ ์šฉ๋„๊ฐ€ ๋‹ฌ๋ผ์งˆ ์ˆ˜ ์žˆ๋‹ค. + - ex. + - mission ํ…Œ์ด๋ธ”์—์„œ์˜ status โ†’ ์ง„ํ–‰ ์ค‘ / ์™„๋ฃŒ + - member ํ…Œ์ด๋ธ”์—์„œ์˜ status โ†’ ํ™œ์„ฑํ™” / ๋น„ํ™œ์„ฑํ™” +3. mission์— d-day๋ฅผ ๊ณ„์‚ฐํ•˜์—ฌ DB์— ์ €์žฅํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค deadline์„ ์ €์žฅํ•˜๊ณ  deadline์—์„œ ํ˜„์žฌ ๋‚ ์งœ๋ฅผ ๋นผ์„œ d-day๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ์— ๋” ํšจ์œจ์ ์ผ ๊ฒƒ ๊ฐ™๋‹ค. +4. ๋‹ค๋Œ€๋‹ค ๊ด€๊ณ„์˜ ๋งคํ•‘(์ค‘๊ฐ„) ํ…Œ์ด๋ธ”์—์„œ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ผ๋ฉด ๊ตณ์ด inner join์„ 2๋ฒˆ ์“ธ ํ•„์š” ์—†์ด ํ•œ ๋ฒˆ์— ๋งคํ•‘ ํ…Œ์ด๋ธ”์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”์ถœํ•œ๋‹ค. + - ex. + ```sql + select mission_id, member_id from member_mission + select book_id, count(*) from likes group by book_id + ``` + - ์œ„์˜ ๋‘ ๊ฒฝ์šฐ๋Š” ๋งคํ•‘ ํ…Œ์ด๋ธ”์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ํ•œ ๋ฒˆ์— ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค. +5. map ํ…Œ์ด๋ธ”์—์„œ ์ง€๋„์˜ ๋ชจ๋“  ์œ„์น˜์— ๋Œ€ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•ด ์‚ฌ์šฉ์ž์˜ ๊ฑฐ์ฃผ์ง€์™€ ์‹๋‹น์˜ ์œ„์น˜๊ฐ€ ์ฐธ์กฐํ•˜๋„๋ก ํ–ˆ๋Š”๋ฐ ์ด๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๊ณผ๋„ํ•˜๊ฒŒ ๋งŽ์ด ์ €์žฅํ•˜๋ฉฐ ๋น„ํšจ์œจ์ ์ธ ๋ฐฉ์‹์ธ ๊ฒƒ ๊ฐ™๋‹ค. ๊ตณ์ด map ํ…Œ์ด๋ธ”์„ ๋”ฐ๋กœ ์ƒ์„ฑํ•  ํ•„์š” ์—†์ด ์™ธ๋ถ€ api๋ฅผ ์‚ฌ์šฉํ•ด ์ง€๋„ ์ •๋ณด๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ๋‚ซ๊ฒ ๋‹ค. +6. [๋ฏธ์…˜ 3]์—์„œ ์œ„์น˜๋Š” ์‚ฌ์šฉ์ž์˜ ์ž…๋ ฅ์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์งˆ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์‚ฌ์šฉ์ž์˜ ์ž…๋ ฅ ๊ฐ’์œผ๋กœ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์ด ์ข‹๊ฒ ๋‹ค. +7. [๋ฏธ์…˜ 2]๋Š” ๋‹จ์ˆœํžˆ ํ•œ ์‹๋‹น์˜ ๋ฆฌ๋ทฐ๋ฅผ ๋ชจ์•„๋ณด๋Š” ํŽ˜์ด์ง€๊ฐ€ ์•„๋‹ˆ๋ผ ์‹๋‹น ์‚ฌ์žฅ์ด ์ž์‹ ์˜ ์‹๋‹น ๋ฆฌ๋ทฐ๋ฅผ ๋ชจ์•„๋ณด๋Š” ๊ฒƒ์ด๋‹ค. restaurant ํ…Œ์ด๋ธ”์— ์‚ฌ์žฅ id ์†์„ฑ์„ ์ถ”๊ฐ€ํ•˜์—ฌ ์‚ฌ์šฉ์ž id๊ฐ€ ์‚ฌ์žฅ id์™€ ์ผ์น˜ํ•˜๋ฉด ์ž์‹ ์˜ ์‹๋‹น ๋ฆฌ๋ทฐ๋ฅผ ๋ชจ์•„๋ณผ ์ˆ˜ ์žˆ๋„๋ก ์ฟผ๋ฆฌ๋ฅผ ์ž‘์„ฑํ•ด์•ผ ํ•œ๋‹ค. (์‚ฌ์žฅ์˜ ์‹๋‹น์€ 1๊ฐœ ์ด์ƒ์ด ๋  ๊ฒƒ์ด๋‹ค) +8. ์ฟผ๋ฆฌ๋ฌธ์„ ๊ฐ€๋…์„ฑ ์žˆ๊ณ  ๊ฐ„๊ฒฐํ•˜๊ฒŒ ๋‚˜ํƒ€๋‚˜๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ์ค„์˜€๋‹ค. + - ex. + ```sql + -- ์„œ๋ธŒ์ฟผ๋ฆฌ ์‚ฌ์šฉ O + select content, image_url + from review as r + join (select * from image) as i + on r.id = i.review_id; + -- ์„œ๋ธŒ์ฟผ๋ฆฌ ์‚ฌ์šฉ X + select content, image_url + from review as r + join image as i + on r.id = i.review_id; + ``` + +**์ˆ˜์ •ํ•œ ERD ์„ค๊ณ„ ๋ฐ MySQL ์ฟผ๋ฆฌ** +![๊ฐ€๊ฒŒ-๋ฐฉ๋ฌธ-๋ฏธ์…˜-ํฌ์ธํŠธ-์„œ๋น„์Šค-์Šคํ„ฐ๋””-ํ›„-์ถ”๊ฐ€์ˆ˜์ •](images/๊ฐ€๊ฒŒ-๋ฐฉ๋ฌธ-๋ฏธ์…˜-ํฌ์ธํŠธ-์„œ๋น„์Šค-์Šคํ„ฐ๋””-ํ›„-์ถ”๊ฐ€์ˆ˜์ •.jpg) +```sql +CREATE TABLE member ( + id BIGINT NOT NULL AUTO_INCREMENT, + location_address TEXT NOT NULL, + email VARCHAR(50) NOT NULL, + phone_number VARCHAR(15) NULL, + member_name VARCHAR(30) NOT NULL, + nickname VARCHAR(30) NOT NULL, + gender INT NOT NULL, + birth varchar(10) NOT NULL, + points BIGINT DEFAULT 0 NOT NULL, + created_at DATETIME(6) NOT NULL, + updated_at DATETIME(6) NOT NULL, + status INT NOT NULL, + inactive_at datetime(6) NULL, + PRIMARY KEY (id) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3; + +CREATE TABLE food_kind ( + id BIGINT NOT NULL AUTO_INCREMENT, + kind VARCHAR(10) NOT NULL, + created_at DATETIME(6) NOT NULL, + updated_at DATETIME(6) NOT NULL, + status INT NOT NULL, + PRIMARY KEY (id) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3; + +CREATE TABLE member_food_kind ( + id BIGINT NOT NULL AUTO_INCREMENT, + member_id BIGINT NOT NULL, + food_kind_id BIGINT NOT NULL, + created_at DATETIME(6) NOT NULL, + updated_at DATETIME(6) NOT NULL, + status INT NOT NULL, + PRIMARY KEY (id), + FOREIGN KEY (member_id) REFERENCES member(id) ON DELETE CASCADE, + FOREIGN KEY (food_kind_id) REFERENCES food_kind(id) ON DELETE CASCADE +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3; + +CREATE TABLE restaurant ( + id BIGINT NOT NULL AUTO_INCREMENT, + ceo_id BIGINT NOT NULL, + location_address TEXT NOT NULL, + restaurant_name VARCHAR(30) NOT NULL, + introduction TEXT NOT NULL, + start_time TIMESTAMP NOT NULL, + end_time TIMESTAMP NOT NULL, + total_rating decimal(2, 1) DEFAULT 0 NOT NULL, + created_at DATETIME(6) NOT NULL, + updated_at DATETIME(6) NOT NULL, + status INT NOT NULL, + PRIMARY KEY (id), + FOREIGN KEY (ceo_id) REFERENCES member(id) ON DELETE CASCADE +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3; + +CREATE TABLE mission ( + id BIGINT NOT NULL AUTO_INCREMENT, + restaurant_id BIGINT NOT NULL, + mission_name VARCHAR(30) NOT NULL, + introduction TEXT NOT NULL, + deadline DATETIME NULL, + points BIGINT NOT NULL, + created_at DATETIME(6) NOT NULL, + updated_at DATETIME(6) NOT NULL, + status INT NOT NULL, + PRIMARY KEY (id), + FOREIGN KEY (restaurant_id) REFERENCES restaurant(id) ON DELETE CASCADE +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3; + +CREATE TABLE member_mission ( + id BIGINT NOT NULL AUTO_INCREMENT, + member_id BIGINT NOT NULL, + mission_id BIGINT NOT NULL, + created_at DATETIME(6) NOT NULL, + updated_at DATETIME(6) NOT NULL, + status INT NOT NULL, + PRIMARY KEY (id), + FOREIGN KEY (member_id) REFERENCES member(id) ON DELETE CASCADE, + FOREIGN KEY (mission_id) REFERENCES mission(id) ON DELETE CASCADE +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3; + +CREATE TABLE review ( + id BIGINT NOT NULL AUTO_INCREMENT, + member_id BIGINT NOT NULL, + restaurant_id BIGINT NOT NULL, + rating decimal(2, 1) NOT NULL, + content TEXT NOT NULL, + created_at DATETIME(6) NOT NULL, + updated_at DATETIME(6) NOT NULL, + status INT NOT NULL, + PRIMARY KEY (id), + FOREIGN KEY (member_id) REFERENCES member(id) ON DELETE CASCADE, + FOREIGN KEY (restaurant_id) REFERENCES restaurant(id) ON DELETE CASCADE +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3; + +CREATE TABLE reply ( + id BIGINT NOT NULL AUTO_INCREMENT, + member_id BIGINT NOT NULL, + review_id BIGINT NOT NULL, + writer VARCHAR(30) NOT NULL, + content TEXT NOT NULL, + created_at DATETIME(6) NOT NULL, + updated_at DATETIME(6) NOT NULL, + status INT NOT NULL, + PRIMARY KEY (id), + FOREIGN KEY (member_id) REFERENCES member(id) ON DELETE CASCADE, + FOREIGN KEY (review_id) REFERENCES review(id) ON DELETE CASCADE +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3; + +CREATE TABLE inquiry ( + id BIGINT NOT NULL AUTO_INCREMENT, + member_id BIGINT NOT NULL, + title VARCHAR(30) NOT NULL, + content TEXT NOT NULL, + is_conformed INT NOT NULL, + created_at DATETIME(6) NOT NULL, + updated_at DATETIME(6) NOT NULL, + status INT NOT NULL, + PRIMARY KEY (id), + FOREIGN KEY (member_id) REFERENCES member(id) ON DELETE CASCADE +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3; + +CREATE TABLE food_kind_restaurant ( + id BIGINT NOT NULL AUTO_INCREMENT, + food_kind_id BIGINT NOT NULL, + restaurant_id BIGINT NOT NULL, + created_at DATETIME(6) NOT NULL, + updated_at DATETIME(6) NOT NULL, + status INT NOT NULL, + PRIMARY KEY (id), + FOREIGN KEY (food_kind_id) REFERENCES food_kind(id) ON DELETE CASCADE, + FOREIGN KEY (restaurant_id) REFERENCES restaurant(id) ON DELETE CASCADE +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3; + +CREATE TABLE image ( + id BIGINT NOT NULL AUTO_INCREMENT, + restaurant_id BIGINT NULL, + inquiry_id BIGINT NULL, + review_id BIGINT NULL, + image_url TEXT NOT NULL, + created_at DATETIME(6) NOT NULL, + updated_at DATETIME(6) NOT NULL, + status INT NOT NULL, + PRIMARY KEY (id), + FOREIGN KEY (restaurant_id) REFERENCES restaurant(id) ON DELETE CASCADE, + FOREIGN KEY (inquiry_id) REFERENCES inquiry(id) ON DELETE CASCADE, + FOREIGN KEY (review_id) REFERENCES review(id) ON DELETE CASCADE +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3; + +CREATE TABLE alarm ( + id BIGINT NOT NULL AUTO_INCREMENT, + member_id BIGINT NOT NULL, + is_conformed INT NOT NULL, + created_at DATETIME(6) NOT NULL, + updated_at DATETIME(6) NOT NULL, + status INT NOT NULL, + PRIMARY KEY (id), + FOREIGN KEY (member_id) REFERENCES member(id) ON DELETE CASCADE +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3; + +CREATE TABLE alarm_new_mission ( + id BIGINT NOT NULL AUTO_INCREMENT, + alarm_id BIGINT NOT NULL, + title VARCHAR(30) NOT NULL, + body TEXT NOT NULL, + status INT NOT NULL, + PRIMARY KEY (id), + FOREIGN KEY (alarm_id) REFERENCES alarm(id) ON DELETE CASCADE +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3; + +CREATE TABLE alarm_review_request ( + id BIGINT NOT NULL AUTO_INCREMENT, + alarm_id BIGINT NOT NULL, + title VARCHAR(30) NOT NULL, + body TEXT NOT NULL, + status INT NOT NULL, + PRIMARY KEY (id), + FOREIGN KEY (alarm_id) REFERENCES alarm(id) ON DELETE CASCADE +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3; +``` + +**๋‚ด๊ฐ€ ์ง„ํ–‰ ์ค‘, ์ง„ํ–‰ ์™„๋ฃŒํ•œ ๋ฏธ์…˜์„ ๋ชจ์•„์„œ ๋ณด๋Š” ์ฟผ๋ฆฌ (ํŽ˜์ด์ง• ํฌํ•จ)** +1. ์ง„ํ–‰ ์ค‘์ธ ๋ฏธ์…˜ ๋ชฉ๋ก + ```sql + select restaurant_name, m.points, m.introduction + from member_mission as mm + inner join mission as m on m.id = mm.mission_id + inner join restaurant as r on m.restaurant_id = r.id + where m.status = 1 and mm.member_id = 1 + order by m.created_at limit 5 offset 0; + ``` + - ํ…Œ์ŠคํŠธ ๊ฒฐ๊ณผ + ![๋ฏธ์…˜1-1-์ฟผ๋ฆฌ์ž‘์„ฑ](images/๋ฏธ์…˜1-1-์ฟผ๋ฆฌ์ž‘์„ฑ.png) +1. ์ง„ํ–‰ ์™„๋ฃŒํ•œ ๋ฏธ์…˜ ๋ชฉ๋ก + ```sql + select restaurant_name, m.points, m.introduction + from member_mission as mm + inner join mission as m on m.id = mm.mission_id + inner join restaurant as r on m.restaurant_id = r.id + where m.status = 2 and mm.member_id = 1 + order by m.created_at limit 5 offset 0; + ``` + - ํ…Œ์ŠคํŠธ ๊ฒฐ๊ณผ + ![๋ฏธ์…˜1-2-์ฟผ๋ฆฌ์ž‘์„ฑ](images/๋ฏธ์…˜1-2-์ฟผ๋ฆฌ์ž‘์„ฑ.png) +(์ƒ์„ฑํ•œ ํ…Œ์ด๋ธ”์— ์ž„์˜์˜ ๋ฐ์ดํ„ฐ๋“ค์„ ์‚ฝ์ž…ํ•˜์—ฌ ํ…Œ์ŠคํŠธํ•ด๋ณด์•˜๋‹ค.) + +**๋ฆฌ๋ทฐ ์ž‘์„ฑํ•˜๋Š” ์ฟผ๋ฆฌ** +1. ๋ฆฌ๋ทฐ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ์ฟผ๋ฆฌ + ```sql + insert into review (id, member_id, restaurant_id, rating, content, created_at, updated_at, status) values + (1, 1, 1, 4.5, '๋ฆฌ๋ทฐ ๋‚ด์šฉ', NOW(), NOW(), 1) + ``` + - ํ…Œ์ŠคํŠธ ๊ฒฐ๊ณผ + ![๋ฏธ์…˜2-1-์ฟผ๋ฆฌ์ž‘์„ฑ](images/๋ฏธ์…˜2-1-์ฟผ๋ฆฌ์ž‘์„ฑ.png) +1. ์‹๋‹น ์‚ฌ์žฅ์ด ์ž์‹ ์˜ ์‹๋‹น ๋ฆฌ๋ทฐ๋ฅผ ๋ชจ์•„๋ณด๋Š” ์ฟผ๋ฆฌ + ```sql + select reviewer.nickname, rating, r.content, image_url, r.created_at, re.content as reply_content + from member as mem + inner join restaurant as rest on rest.ceo_id = mem.id + inner join review as r on r.restaurant_id = rest.id + left join reply as re on r.id = re.review_id + left join image as i on r.id = i.review_id + inner join member as reviewer on reviewer.id = r.member_id + WHERE mem.id = 1 + order by r.created_at desc, re.created_at desc, i.created_at desc limit 5 offset 0; + ``` + - id๊ฐ€ 1์ธ ์‚ฌ์šฉ์ž(์‚ฌ์žฅ)๊ฐ€ ์ž์‹ ์˜ ์‹๋‹น๋“ค์˜ ๋ฆฌ๋ทฐ๋“ค์„ ๋ชจ์•„๋ณธ๋‹ค. + - ํ…Œ์ŠคํŠธ ๊ฒฐ๊ณผ + ![๋ฏธ์…˜2-2-์ฟผ๋ฆฌ์ž‘์„ฑ](images/๋ฏธ์…˜2-2-์ฟผ๋ฆฌ์ž‘์„ฑ.png) + +**ํ™ˆ ํ™”๋ฉด ์ฟผ๋ฆฌ (ํ˜„์žฌ ์„ ํƒ ๋œ ์ง€์—ญ์—์„œ ๋„์ „์ด ๊ฐ€๋Šฅํ•œ ๋ฏธ์…˜ ๋ชฉ๋ก, ํŽ˜์ด์ง• ํฌํ•จ)** +1. ํ˜„์žฌ ์„ ํƒ๋œ ์ง€์—ญ(์‚ฌ์šฉ์ž ๊ฑฐ์ฃผ์ง€) ๋ฐ ์ง„ํ–‰ ์™„๋ฃŒํ•œ ๋ฏธ์…˜์˜ ๊ฐœ์ˆ˜ + ```sql + select location_address, count(mi.id) as mission_completed + from member as m + left join member_mission as mm on m.id = mm.member_id + left join mission as mi on mm.mission_id = mi.id and mi.status = 2 + where m.id = 4 + ``` + - left join์„ ์‚ฌ์šฉํ•ด ์‚ฌ์šฉ์ž๊ฐ€ ์™„๋ฃŒํ•œ ๋ฏธ์…˜์ด ์—†๋”๋ผ๋„ member ํ…Œ์ด๋ธ”์˜ ํ–‰์€ ์œ ์ง€๋˜๊ณ  count(mi.id)์œผ๋กœ mi.status = 1์ธ mission๋งŒ ์ง‘๊ณ„ํ•จ์œผ๋กœ์จ ๋ฏธ์…˜ ์™„๋ฃŒ ์—ฌ๋ถ€์™€ ์ƒ๊ด€์—†์ด location_address๋Š” ํ•ญ์ƒ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋„๋ก ํ•˜์˜€๋‹ค. ์™„๋ฃŒํ•œ ๋ฏธ์…˜์ด ์—†๋Š” ๊ฒฝ์šฐ์—๋„ location_address๋Š” ์œ ์ง€๋˜๊ณ  mission_completed๋Š” 0์œผ๋กœ ํ‘œ์‹œ๋œ๋‹ค. + - ํ…Œ์ŠคํŠธ ๊ฒฐ๊ณผ + ![๋ฏธ์…˜3-1-์ฟผ๋ฆฌ์ž‘์„ฑ](images/๋ฏธ์…˜3-1-์ฟผ๋ฆฌ์ž‘์„ฑ.png) +2. ํ˜„์žฌ ์„ ํƒ ๋œ ์ง€์—ญ(์ž…๋ ฅ ๊ฐ’์œผ๋กœ ๋„ฃ์€ ์ง€์—ญ)์—์„œ ๋„์ „์ด ๊ฐ€๋Šฅํ•œ ๋ฏธ์…˜ ๋ชฉ๋ก + ```sql + select restaurant_name, kind, m.introduction, m.points, datediff(m.deadline, now()) as dday + from member_mission as mm + inner join mission as m on mm.mission_id = m.id + inner join restaurant as r on r.id = m.restaurant_id + inner join food_kind_restaurant as fkr on fkr.restaurant_id = r.id + inner join food_kind as fk on fk.id = fkr.food_kind_id + where member_id = 4 and m.status = 0 and location_address like '%Maple%' + order by m.created_at limit 5 offset 0; + ``` + - id๊ฐ€ 4์ธ ์‚ฌ์šฉ์ž์˜ ํ˜„์žฌ ์„ ํƒ๋œ ์ง€์—ญ์ธ Maple์—์„œ ๋„์ „์ด ๊ฐ€๋Šฅํ•œ ๋ฏธ์…˜์„ ์กฐํšŒํ•œ๋‹ค. + - Maple์ด๋ผ๋Š” ์ง€์—ญ์ด ํฌํ•จ๋œ ์ฃผ์†Œ์— ์œ„์น˜ํ•œ ์‹๋‹น์˜ ๋ฏธ์…˜๋“ค์„ ๊ฐ€์ ธ์˜ค๋„๋ก ์ž‘์„ฑํ•˜์˜€๋‹ค . + - ํ…Œ์ŠคํŠธ ๊ฒฐ๊ณผ + ![๋ฏธ์…˜3-2-์ฟผ๋ฆฌ์ž‘์„ฑ](images/๋ฏธ์…˜3-2-์ฟผ๋ฆฌ์ž‘์„ฑ.png) + +**๋งˆ์ด ํŽ˜์ด์ง€ ํ™”๋ฉด ์ฟผ๋ฆฌ** +```sql +select nickname, email, phone_number, points +from member +where id = 1 +``` +- ํ…Œ์ŠคํŠธ ๊ฒฐ๊ณผ + ![๋ฏธ์…˜4-์ฟผ๋ฆฌ์ž‘์„ฑ](images/๋ฏธ์…˜4-์ฟผ๋ฆฌ์ž‘์„ฑ.png) + +--- +**ํ›„๊ธฐ** +- ์ฒ˜์Œ ๋ฏธ์…˜์„ ์ˆ˜ํ–‰ํ–ˆ์„ ๋•Œ ์™œ ์ด๋ ‡๊ฒŒ ๋จธ๋ฆฌ๊ฐ€ ์•„ํŒ ๋‚˜ ํ–ˆ๋”๋‹ˆ ์“ธ๋ฐ์—†์ด join๊ณผ ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ๋‚จ๋ฐœํ•ด์„œ์˜€๋‹ค. ๋„ˆ๋ฌด ๋ณต์žกํ•˜๊ฒŒ ์ž‘์„ฑํ•ด์„œ ์Šคํ„ฐ๋””์›๋“ค์—๊ฒŒ ๋‚ด๊ฐ€ ์“ด ์ฟผ๋ฆฌ๋ฅผ ์„ค๋ช…ํ•  ๋•Œ์—๋„ ํž˜์ด ๋ถ€์ณค๋‹ค. ์Šคํ„ฐ๋””์›๋“ค์ด ํ•ด์ค€ ์กฐ์–ธ๋“ค์„ ๋ฐ”ํƒ•์œผ๋กœ ์ฟผ๋ฆฌ๋ฌธ์„ ์ˆ˜์ •ํ•˜๋‹ˆ ํ›จ์”ฌ ๊ฐ€๋…์„ฑ์žˆ๊ณ  ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค. ์„œ๋กœ ํ† ๋ก ํ•˜๋ฉฐ ๊ณต๋ถ€ํ•˜๋Š” ์Šคํ„ฐ๋””์˜ ์ค‘์š”์„ฑ์„ ๋‹ค์‹œ ํ•œ ๋ฒˆ ๋Š๋‚€๋‹ค. ๐Ÿฅฒ \ No newline at end of file