[MySQL] sql์˜ ๐ŸŒธJOIN [์ˆ˜์ •ํ•„์š”]

๊น€๊ธฐ์šฉยท2020๋…„ 10์›” 15์ผ
0
post-thumbnail

NoSQL: ์€ ๋”•์…”๋„ˆ๋ฆฌ ํ˜•ํƒœ๋กœ ์ƒ๊น€
์žฅ์  - ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋ถ€ ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค.(๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋ณ€ํ™”์— ์œ ์—ฐํ•จ)
- ์†๋„๊ฐ€ ๋น ๋ฅด๋‹ค.

1์ฐจ ์ •๊ทœ

2์ฐจ ์ •๊ทœ : ๋ชจ๋“  ์†์„ฑ์€ ๊ธฐ๋ณธํ‚ค์— ์ข…์†

one to one:
ํ…Œ์ด๋ธ” A์˜ ๋กœ์šฐ์™€ ํ…Œ์ด๋ธ” B์˜ ๋กœ์šฐ๊ฐ€ ์ •ํ™•ํžˆ ์ผ๋Œ€์ผ ๋งค์นญ์ด ๋˜๋Š”๊ด€๊ณ„

one to many:
ํ…Œ์ด๋ธ” A์˜ ๋กœ์šฐ๊ฐ€ ํ…Œ์ด๋ธ” B์˜ ์—ฌ๋Ÿฌ ๋กœ์šฐ์™€ ์—ฐ๊ฒฐ์ด ๋˜๋Š” ๊ด€๊ณ„

many to many:
ํ…Œ์ด๋ธ” A์˜ ์—ฌ๋Ÿฌ ๋กœ์šฐ๊ฐ€ ํ…Œ์ด๋ธ” B์˜ ์—ฌ๋Ÿฌ ๋กœ์šฐ์™€ ์—ฐ๊ฒฐ์ด ๋˜๋Š” ๊ด€๊ณ„
(๋ฌผ๋ฆฌ์ ์œผ๋กœ ํ‘œํ˜„ํ• ๋•Œ ์ค‘๊ฐ„ํ…Œ์ด๋ธ”์ด ํ•„์š”ํ•˜๋‹ค.)
1. ์ˆซ์ž๋กœ ์ €์žฅํ•œ๋‹ค(๋ฉ”๋ชจ๋ฆฌ์ ˆ์•ฝ)
2. ์กฐํ•ฉ์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์—ฐ๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.
3. id ๊ฐ’์„ ์ฐธ์กฐํ•˜๊ธฐ๋•Œ๋ฌธ์— id์— ์†ํ•œ ๋‚ด์šฉ๋งŒ ๋ณ€๊ฒฝํ•ด์ฃผ๋ฉด ๋ชจ๋“  ํ…Œ์ด๋ธ”์˜ ๊ฐ’์„ ์ˆ˜์ •ํ• ์ˆ˜ ์žˆ๋‹ค.(ํŽธํ•œ ๋ฐ์ดํ„ฐ ์œ ์ง€๋ณด์ˆ˜)


ERD (Entity Relationship Diagram)
๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๊ฐ€ ์‹œ๊ฐํ™”๋œ ๋„ํ‘œ
์Šคํƒ€๋ฒ…์Šค
1. ์นดํ…Œ๊ณ ๋ฆฌ
-
-
2. ์ œํ’ˆ
-
-

  1. ์žฌ๋ฃŒ

๊ณต๋ถ€.


ํ…Œ์ด๋ธ” ๋ฐฑ์—…ํ•˜๊ธฐ

RENAME TABLE topic TO topic_backup;

JOIN์˜ ์ข…๋ฅ˜

"Every table in the database need to have a single theme"
DATA Table ์„ ๋ณผ๋•Œ ์ค‘๋ณต๋˜๋Š” ๋ ˆ์ฝ”๋“œ๋Š” ๊ผญ ์œ ์ง€๋ณด์ˆ˜๋ฅผ ์œ„ํ•ด์„œ ๋ถ„๋ฆฌ๋ฅผ ํ•ด์•ผํ•œ๋‹ค.

Topic

idtitledescriptionnamecityjob_titlejob_description
1HTMLHTML is...AmuseSeouldeveloperdeveloper is...
2CSSCSS is...GokuJejuactoractor is ...
3DATABASEDATABASE is...EllieYoutubedeveloperdeveloper is ...

ํ…Œ์ด๋ธ” ๋ถ„๋ฆฌ

Topic

tidtitledescriptionauthor_id
1HTMLHTML is...1
2CSSCSS is...2
3DATABASEDATABASE is...3
4OracleOracle is ...NULL

author

aidnamecityprofile_id
1AmuseSeoul1
2GokuJeju2
3EllieYoutube3

profile

pidtittlejob_description
1developerdeveloper is ...
2actoractor is ...

LEFT JOIN

SELECT * FROM topic LEFT JOIN author ON topic.author_id = author.aid
idtitledescriptionauthor_idaidnamecityprofile_id
1HTMLHTML is...11Amuseseoul1
2CSSCSS is...22GokuJeju2
3DATABASEDATABASE is...33EllieYoutube1
4OracleOracle is ...NULLNULLNULLNULLNULL
SELECT * FROM topic LEFT JOIN author ON topic.author_id = author.aid LEFT JOIN profile ON author.profile_id = profile.pid;
idtitledescriptionauthor_idaidnamecityprofile_idpidtitlejob_description
1HTMLHTML is...11Amuseseoul11developerdeveloper is...
2CSSCSS is...22GokuJeju22actoractor is ...
3DATABASEDATABASE is...33EllieYoutube11developerdeveloper is ...
4OracleOracle is ...NULLNULLNULLNULLNULLNULLNULLNULL
SELECT topic.title, topic.description, name, city, profile.title AS job FROM topic LEFT JOIN author ON topic.author_id = author.aid LEFT JOIN profile ON author.profile_id = profile.pid;
titledescriptionnamecityjob
HTMLHTML is...Amuseseouldeveloper
CSSCSS is...GokuJejuactor
DATABASEDATABASE is...EllieYoutubedeveloper

INNER JOIN

SELECT * FROM topic INNER JOIN author ON topic.author_id = author.aid;

FULL OUTER JOIN

SELECT * FROM topic FULL OUTER JOIN author ON topic.author_id = author.aid;

EXCLUSIVE JOIN


Reference
https://sql-joins.leopard.in.ua/
https://opentutorials.org/course/3884/25180

profile
๋งค์ผ ์ƒˆ๋กœ์šด ๋ฐฐ์›€์„ ํ†ตํ•ด ๊พธ์ค€ํžˆ ์„ฑ์žฅํ•˜๋Š” ๊ฒƒ์„ ๋ชฉํ‘œ๋ฅผ ๋‘๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋…ผ๋ฆฌ์ ์ธ ์‚ฌ๊ณ ๋กœ ๋ฌธ์ œํ•ด๊ฒฐ ํ•˜๋Š”๊ฒƒ์— ํฌ์—ด์„ ๋Š๋ผ๊ณ  ์–ธ์  ๊ฐ€ ์ œ๊ฐ€ ๋งŒ๋“  ๊ฒฐ๊ณผ๋ฌผ๋“ค์ด ์‚ฌ๋žŒ๋“ค์—๊ฒŒ ํŽธ์ด๋ฅผ ์ œ๊ณตํ•˜๋ฉฐ ์‚ฌ์šฉ๋˜๋Š” ๋‚ ์„ ๊ฐ„์ ˆํžˆ ์†Œ๋งํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๐Ÿ™

0๊ฐœ์˜ ๋Œ“๊ธ€