post-custom-banner

โœ๐Ÿป 23์ผ ๊ณต๋ถ€ ์ด์•ผ๊ธฐ.



UNION

์‹ค์Šต ํ™˜๊ฒฝ ๋งŒ๋“ค๊ธฐ

USE zerobase
<-- celeb ํ…Œ์ด๋ธ” ํ™•์ธ -->

<-- ํ…Œ์ŠคํŠธ์šฉ ํ…Œ์ด๋ธ” ์ƒ์„ฑ -->
CREATE TABLE test1
(
	no int
)
CREATE TABLE test2
(
	no int
)

<-- ๋ฐ์ดํ„ฐ ์ž…๋ ฅ -->
INSERT INTO test1 VALUES (1);
INSERT INTO test1 VALUES (2);
INSERT INTO test1 VALUES (3);

INSERT INTO test2 VALUES (5);
INSERT INTO test2 VALUES (6);
INSERT INTO test2 VALUES (3);

<-- test1 & test2 ํ…Œ์ด๋ธ” ํ™•์ธ -->

Union

์—ฌ๋Ÿฌ ๊ฐœ์˜ SQL๋ฌธ์„ ํ•ฉ์ณ์„œ ํ•˜๋‚˜์˜ SQL ๋ฌธ์œผ๋กœ ๋งŒ๋“ค์–ด์ค€๋‹ค. ์ด๋•Œ, ๊ฐ SQL๋ฌธ์—์„œ ๊ฐ€์ ธ์˜ค๋Š” ์นผ๋Ÿผ์˜ ๊ฐœ์ˆ˜๊ฐ€ ๊ฐ™์•„์•ผํ•œ๋‹ค. (์ˆ˜์ง๊ฒฐํ•ฉ)

  • UNION : ์ค‘๋ณต๋œ ๊ฐ’์„ ์ œ๊ฑฐํ•˜์—ฌ ์•Œ๋ ค์คŒ
  • UNION ALL : ์ค‘๋ณต๋œ ๊ฐ’๋„ ๋ชจ๋‘ ์•Œ๋ ค์คŒ
SELECT ์ปฌ๋Ÿผ์ด๋ฆ„1, ์ปฌ๋Ÿผ์ด๋ฆ„2 , ... FROM ํ…Œ์ด๋ธ”์ด๋ฆ„1
UNION | UNION ALL
SELECT ์ปฌ๋Ÿผ์ด๋ฆ„1, ์ปฌ๋Ÿผ์ด๋ฆ„2 , ... FROM ํ…Œ์ด๋ธ”์ด๋ฆ„2
UNION UNION ALL

์˜ˆ์ œ) ์„ฑ์ด ์ด์”จ์ธ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฒ€์ƒ‰ํ•˜๋Š” ์ฟผ๋ฆฌ์™€ 1970๋…„๋Œ€ ์ƒ์„ ๊ฒ€์ƒ‰ํ•˜๋Š” ์ฟผ๋ฆฌ๋ฅผ ์ค‘๋ณต์„ ํฌํ•จํ•˜์—ฌ ์กฐํšŒํ•˜๋ผ.




JOIN

์‹ค์Šตํ™˜๊ฒฝ ๋งŒ๋“ค๊ธฐ

USE zerobase

<-- celeb ํ…Œ์ด๋ธ” ํ™•์ธ -->

<-- ํ…Œ์ด๋ธ” ์ƒ์„ฑ -->
CREATE TABLE snl_show
(
	ID int NOT NULL AUTO_INCREMENT PRIMARY KEY,
    SEASON int NOT NULL,
    EPISODE int NOT NULL,
    BROADCAST_DATE date,
    HOST varchar(32) NOT NULL
);

<-- ๋ฐ์ดํ„ฐ ์ž…๋ ฅ -->
<-- ํ…Œ์ด๋ธ” ํ™•์ธ -->
celeb snl_show

JOIN

๋‘ ๊ฐœ ์ด์ƒ์˜ ํ…Œ์ด๋ธ”์„ ๊ฒฐํ•ฉํ•ด์ค€๋‹ค. (์ˆ˜ํ‰๊ฒฐํ•ฉ)

๐Ÿ“Œ ํ…Œ์ด๋ธ”A์˜ ์ปฌ๋Ÿผ1์€ ํ…Œ์ด๋ธ”A.์ปฌ๋Ÿผ1๊ณผ ๊ฐ™์ด ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

INNER JOIN

๋‘ ๊ฐœ์˜ ํ…Œ์ด๋ธ”์—์„œ ๊ณตํ†ต๋œ ์š”์†Œ๋“ค์„ ๊ฒฐํ•ฉํ•ด์ค€๋‹ค.

SELECT ์ปฌ๋Ÿผ์ด๋ฆ„1, ์ปฌ๋Ÿผ์ด๋ฆ„2, ...
FROM ํ…Œ์ด๋ธ”์ด๋ฆ„1
INNER JOIN ํ…Œ์ด๋ธ”์ด๋ฆ„2
ON ํ…Œ์ด๋ธ”์ด๋ฆ„1.์ปฌ๋Ÿผ์ด๋ฆ„ = ํ…Œ์ด๋ธ”์ด๋ฆ„2.์ปฌ๋Ÿผ์ด๋ฆ„ <-- ๊ธฐ์ค€ -->
WHERE ์กฐ๊ฑด

์˜ˆ์ œ) snl_show์— ํ˜ธ์ŠคํŠธ๋กœ ์ถœํ˜„ํ•œ celeb์„ ๊ธฐ์ค€์œผ๋กœ celeb ํ…Œ์ด๋ธ”๊ณผ snl_show ํ…Œ์ด๋ธ”์„ inner join ํ•˜๋ผ.


FULL OUTER JOIN(MySQL์—์„œ ์ง€์› โŒ)

๋‘ ๊ฐœ์˜ ํ…Œ์ด๋ธ”์—์„œ ๊ณตํ†ต์˜์—ญ์„ ํฌํ•จํ•˜์—ฌ ์–‘์ชฝ ํ…Œ์ด๋ธ”์˜ ๋‹ค๋ฅธ ์˜์—ญ์„ ๋ชจ๋“  ๋ชจํ•จํ•˜์—ฌ ๊ฒฐํ•ฉํ•ด์ค€๋‹ค.

SELECT ์ปฌ๋Ÿผ์ด๋ฆ„1, ์ปฌ๋Ÿผ์ด๋ฆ„2, ...
FROM ํ…Œ์ด๋ธ”์ด๋ฆ„1
FULL OUTER JOIN ํ…Œ์ด๋ธ”์ด๋ฆ„2
ON ํ…Œ์ด๋ธ”์ด๋ฆ„1.์ปฌ๋Ÿผ์ด๋ฆ„ = ํ…Œ์ด๋ธ”์ด๋ฆ„2.์ปฌ๋Ÿผ์ด๋ฆ„ <-- ๊ธฐ์ค€ -->
WHERE ์กฐ๊ฑด

MySQL์—์„œ๋Š” ์ง€์›ํ•˜์ง€ ์•Š๋Š” ๋ฌธ๋ฒ•์ด๋ฏ€๋กœ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•ด์ค˜์•ผํ•œ๋‹ค.

SELECT ์ปฌ๋Ÿผ์ด๋ฆ„1, ์ปฌ๋Ÿผ์ด๋ฆ„2, ...
FROM ํ…Œ์ด๋ธ”์ด๋ฆ„1
LEFT JOIN ํ…Œ์ด๋ธ”์ด๋ฆ„2 ON ํ…Œ์ด๋ธ”์ด๋ฆ„1.์ปฌ๋Ÿผ์ด๋ฆ„ = ํ…Œ์ด๋ธ”์ด๋ฆ„2.์ปฌ๋Ÿผ์ด๋ฆ„
UNION
SELECT ์ปฌ๋Ÿผ์ด๋ฆ„1, ์ปฌ๋Ÿผ์ด๋ฆ„2, ...
FROM ํ…Œ์ด๋ธ”์ด๋ฆ„1
RIGHT JOIN ํ…Œ์ด๋ธ”์ด๋ฆ„2 ON ํ…Œ์ด๋ธ”์ด๋ฆ„1.์ปฌ๋Ÿผ์ด๋ฆ„ = ํ…Œ์ด๋ธ”์ด๋ฆ„2.์ปฌ๋Ÿผ์ด๋ฆ„
WHERE ์กฐ๊ฑด

์˜ˆ์ œ) snl_show์— ํ˜ธ์ŠคํŠธ๋กœ ์ถœํ˜„ํ•œ celeb์„ ๊ธฐ์ค€์œผ๋กœ celeb ํ…Œ์ด๋ธ”๊ณผ snl_show ํ…Œ์ด๋ธ”์„ full outer join ํ•˜๋ผ.


LEFT JOIN

๋‘ ๊ฐœ์˜ ํ…Œ์ด๋ธ”์—์„œ ๊ณตํ†ต์˜์—ญ์„ ํฌํ•จํ•ด ์™ผ์ชฝ ํ…Œ์ด๋ธ”์˜ ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ๋ฅผ ํฌํ•จํ•˜์—ฌ ๊ฒฐํ•ฉํ•ด์ค€๋‹ค.

SELECT ์ปฌ๋Ÿผ์ด๋ฆ„1, ์ปฌ๋Ÿผ์ด๋ฆ„2, ...
FROM ํ…Œ์ด๋ธ”์ด๋ฆ„1 <-- LEFT TABLE -->
LEFT JOIN ํ…Œ์ด๋ธ”์ด๋ฆ„2
ON ํ…Œ์ด๋ธ”์ด๋ฆ„1.์ปฌ๋Ÿผ์ด๋ฆ„ = ํ…Œ์ด๋ธ”์ด๋ฆ„2.์ปฌ๋Ÿผ์ด๋ฆ„ <-- ๊ธฐ์ค€ -->
WHERE ์กฐ๊ฑด

์˜ˆ์ œ) snl_show์— ํ˜ธ์ŠคํŠธ๋กœ ์ถœํ˜„ํ•œ celeb์„ ๊ธฐ์ค€์œผ๋กœ celeb ํ…Œ์ด๋ธ”๊ณผ snl_show ํ…Œ์ด๋ธ”์„ left join ํ•˜๋ผ.


RIGHT JOIN

๋‘ ๊ฐœ์˜ ํ…Œ์ด๋ธ”์—์„œ ๊ณตํ†ต์˜์—ญ์„ ํฌํ•จํ•ด ์˜ค๋ฅธ์ชฝ ํ…Œ์ด๋ธ”์˜ ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ๋ฅผ ํฌํ•จํ•˜์—ฌ ๊ฒฐํ•ฉํ•ด์ค€๋‹ค.

SELECT ์ปฌ๋Ÿผ์ด๋ฆ„1, ์ปฌ๋Ÿผ์ด๋ฆ„2, ...
FROM ํ…Œ์ด๋ธ”์ด๋ฆ„1
RIGHT JOIN ํ…Œ์ด๋ธ”์ด๋ฆ„2 <-- RIGHT TABLE -->
ON ํ…Œ์ด๋ธ”์ด๋ฆ„1.์ปฌ๋Ÿผ์ด๋ฆ„ = ํ…Œ์ด๋ธ”์ด๋ฆ„2.์ปฌ๋Ÿผ์ด๋ฆ„ <-- ๊ธฐ์ค€ -->
WHERE ์กฐ๊ฑด

์˜ˆ์ œ) snl_show์— ํ˜ธ์ŠคํŠธ๋กœ ์ถœํ˜„ํ•œ celeb์„ ๊ธฐ์ค€์œผ๋กœ celeb ํ…Œ์ด๋ธ”๊ณผ snl_show ํ…Œ์ด๋ธ”์„ right join ํ•˜๋ผ.


SELF JOIN

๋‘ ๊ฐœ์˜ ํ…Œ์ด๋ธ”์—์„œ ๊ธฐ์ค€์— ๋งž๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ํฌํ•จํ•˜์—ฌ ๊ฒฐํ•ฉํ•ด์ค€๋‹ค.

SELECT ์ปฌ๋Ÿผ์ด๋ฆ„1, ์ปฌ๋Ÿผ์ด๋ฆ„2, ...
FROM ํ…Œ์ด๋ธ”์ด๋ฆ„1, ํ…Œ์ด๋ธ”์ด๋ฆ„2 <-- ํ…Œ์ด๋ธ”์ด ์—ฌ๋Ÿฌ๊ฐœ ๋“ค์–ด๊ฐ -->
WHERE ์กฐ๊ฑด <-- ๊ธฐ์ค€ -->

์˜ˆ์ œ) snl_show์— ํ˜ธ์ŠคํŠธ๋กœ ์ถœํ˜„ํ•œ celeb์„ ๊ธฐ์ค€์œผ๋กœ celeb ํ…Œ์ด๋ธ”๊ณผ snl_show ํ…Œ์ด๋ธ”์„ self join ํ•˜๋ผ.




์˜ˆ์ œ) celeb ํ…Œ์ด๋ธ”์˜ ์—ฐ์˜ˆ์ธ ์ค‘, snl_show์— host๋กœ ์ถœํ˜„ํ–ˆ๊ณ , ์˜ํ™”๋ฐฐ์šฐ๋Š” ์•„๋‹ˆ๋ฉด์„œ YG์—”ํ„ฐํ…Œ์ด๋จผํŠธ ์†Œ์†์ด๊ฑฐ๋‚˜, 40์„ธ ์ด์ƒ์ด๋ฉด์„œ YG์—”ํ„ฐํ…Œ์ด๋จผํŠธ ์†Œ์†์ด ์•„๋‹Œ ์—ฐ์˜ˆ์ธ์˜ ์ด๋ฆ„๊ณผ ๋‚˜์ด, ์ง์—…, ์†Œ์†์‚ฌ, ์‹œ์ฆŒ, ์—ํ”ผ์†Œ๋“œ ์ •๋ณด๋ฅผ ์กฐํšŒํ•˜๋ผ.

profile
๋ฐ์ดํ„ฐ ๋ถ„์„๊ฐ€(๊ฐ€ ๋˜๊ณ ํ”ˆ) ํ™ฉ์„ฑ๋ฏธ์ž…๋‹ˆ๋‹ค!
post-custom-banner

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