๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
DBMS/Mariadb

[DBMS]MariaDB Union: ๋ฐ์ดํ„ฐ ํ•ฉ์น˜๊ธฐ์˜ ๊ฐ•๋ ฅํ•œ ๋„๊ตฌ

by YJ Dev 2024. 6. 5.
728x90
๋ฐ˜์‘ํ˜•
SMALL

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ๋‹ค๋ฃจ๋‹ค ๋ณด๋ฉด ์—ฌ๋Ÿฌ ํ…Œ์ด๋ธ”์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•˜๊ณ  ์ด๋ฅผ ํ•˜๋‚˜์˜ ๊ฒฐ๊ณผ๋กœ ํ†ตํ•ฉํ•ด์•ผ ํ•  ๋•Œ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. ์ด๋•Œ MariaDB์˜ Union ์—ฐ์‚ฐ์ž๋Š” ๋งค์šฐ ์œ ์šฉํ•œ ๋„๊ตฌ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” Union์˜ ๊ธฐ๋ณธ ๊ฐœ๋…๋ถ€ํ„ฐ ํ™œ์šฉ๋ฒ•, ์ฃผ์˜์‚ฌํ•ญ๊นŒ์ง€ ์ž์„ธํžˆ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

MariaDB UNION


UNION๊ณผ JOIN์€ ์œ ์‚ฌํ•œ ๊ธฐ๋Šฅ์„ ํ•˜์ง€๋งŒ ์ค‘์š”ํ•œ ์ฐจ์ด๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. UNION์€ ๋‘ ๊ฐœ ์ด์ƒ์˜ SELECT ๊ฒฐ๊ณผ๋ฅผ ๊ฒฐํ•ฉํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ JOIN์€ ๋‘ ํ…Œ์ด๋ธ”์˜ ๊ด€๋ จ ๋ฐ์ดํ„ฐ๋ฅผ ํ•จ๊ป˜ ๊ฒฐํ•ฉํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. JOIN์— ๊ด€ํ•œ ๋‚ด์šฉ์€ ์•„๋ž˜ ํฌ์ŠคํŒ…์„ ์ฐธ๊ณ ํ•ด ์ฃผ์„ธ์š”

" "

[DBMS]MariaDB JOIN: ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํ…Œ์ด๋ธ”์„ ํšจ๊ณผ์ ์œผ๋กœ ๊ฒฐํ•ฉํ•˜๋Š” ๋ฐฉ๋ฒ•

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ์ •๋ณด๋ฅผ ์ถ”์ถœํ•˜๊ฑฐ๋‚˜ ๋ถ„์„ํ•  ๋•Œ ์ข…์ข… ์—ฌ๋Ÿฌ ํ…Œ์ด๋ธ”์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฒฐํ•ฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ JOIN์€ ํ•„์ˆ˜์ ์ธ ๋„๊ตฌ์ž…๋‹ˆ๋‹ค. MariaDB์—์„œ JOIN์„ ์‚ฌ์šฉํ•˜๋ฉด ๋‘ ๊ฐœ ์ด์ƒ์˜ ํ…Œ์ด๋ธ”์„ ํšจ๊ณผ์ ์œผ๋กœ

creativevista.tistory.com

UNION ๊ธฐ๋ณธ ๊ฐœ๋…๊ณผ ์ข…๋ฅ˜๐Ÿ“š

1. UNION์˜ ์ •์˜

UNION์€ ๋‘ ๊ฐœ ์ด์ƒ์˜ SELECT ๋ฌธ ๊ฒฐ๊ณผ๋ฅผ ๊ฒฐํ•ฉํ•˜์—ฌ ํ•˜๋‚˜์˜ ๊ฒฐ๊ณผ ์ง‘ํ•ฉ์œผ๋กœ ๋งŒ๋“œ๋Š” SQL ์—ฐ์‚ฐ์ž์ž…๋‹ˆ๋‹ค. ์ด ์—ฐ์‚ฐ์ž๋ฅผ ํ†ตํ•ด ์—ฌ๋Ÿฌ ํ…Œ์ด๋ธ”์ด๋‚˜ ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ๋ฅผ ํ•˜๋‚˜๋กœ ํ•ฉ์น  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๊ธฐ๋ณธ ๋ฌธ๋ฒ•

SELECT `column1`, `column2` FROM `table1`
UNION
SELECT `column1`, `column2` FROM `table2`;

์žฅ์ 

  • ๋ฐ์ดํ„ฐ ํ†ตํ•ฉ:์—ฌ๋Ÿฌ ํ…Œ์ด๋ธ”์˜ ๋ฐ์ดํ„ฐ๋ฅผ ํ•˜๋‚˜์˜ ๊ฒฐ๊ณผ๋กœ ๊ฒฐํ•ฉํ•ฉ๋‹ˆ๋‹ค.
  • ์ค‘๋ณต ์ œ๊ฑฐ:Union์„ ์‚ฌ์šฉํ•˜์—ฌ ์ค‘๋ณต ๋ฐ์ดํ„ฐ๋ฅผ ์‰ฝ๊ฒŒ ์ œ๊ฑฐ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.
  • ํšจ์œจ์„ฑ:๋ณต์žกํ•œ ์ฟผ๋ฆฌ๋ฅผ ๊ฐ„๋‹จํ•˜๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


์˜ˆ์ œ ๋ฐ์ดํ„ฐ

์•„๋ž˜๋Š” UNION๊ณผ UNION ALL ์˜ˆ์ œ์— ์‚ฌ์šฉํ•  customers ํ…Œ์ด๋ธ”๊ณผ suppliers ํ…Œ์ด๋ธ”์˜ ์˜ˆ์ œ ๋ฐ์ดํ„ฐ์ž…๋‹ˆ๋‹ค.


customers ํ…Œ์ด๋ธ”

id name city
1 Alice New York
2 Bob Los Angeles
3 Charlie New York
4 David Chicago


suppliers ํ…Œ์ด๋ธ”

id company_name city
1 Supplier A New York
2 Supplier B Chicago
3 Supplier C Boston
4 Supplier D San Francisco

2. UNION์˜ ์ข…๋ฅ˜

UNION

Union ์—ฐ์‚ฐ์ž๋Š” ๋‘ ๊ฐœ ์ด์ƒ์˜ SELECT ๋ฌธ์—์„œ ๋ฐ˜ํ™˜๋œ ๊ฒฐ๊ณผ ์ง‘ํ•ฉ์„ ๊ฒฐํ•ฉํ•˜๊ณ , ์ค‘๋ณต๋œ ํ–‰์„ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ์ตœ์ข… ๊ฒฐ๊ณผ ์ง‘ํ•ฉ์— ์ค‘๋ณต๋œ ํ–‰์ด ์—†์Œ์„ ๋ณด์žฅํ•ฉ๋‹ˆ๋‹ค.


ํŠน์ง•

  • ์ค‘๋ณต ์ œ๊ฑฐ: ๋™์ผํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์—ฌ๋Ÿฌ ๋ฒˆ ์„ ํƒํ•œ ๊ฒฝ์šฐ, ํ•œ ๋ฒˆ๋งŒ ๊ฒฐ๊ณผ์— ํฌํ•จ๋ฉ๋‹ˆ๋‹ค.
  • ์ •๋ ฌ: Union์€ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•˜๋Š” ๊ณผ์ •์—์„œ ๊ฒฐ๊ณผ๋ฅผ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋ช…์‹œ์ ์œผ๋กœ ์ •๋ ฌ ์ˆœ์„œ๋ฅผ ์ง€์ •ํ•˜๋ ค๋ฉด ORDER BY ์ ˆ์„ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
SELECT `city` FROM `customers`
UNION
SELECT `city` FROM `suppliers`;


๊ฒฐ๊ณผ

city
New York
Los Angeles
Chicago
Boston
San Francisco

UNION ALL

Union All ์—ฐ์‚ฐ์ž๋Š” ๋‘ ๊ฐœ ์ด์ƒ์˜ SELECT ๋ฌธ์—์„œ ๋ฐ˜ํ™˜๋œ ๊ฒฐ๊ณผ ์ง‘ํ•ฉ์„ ๊ฒฐํ•ฉํ•˜๋ฉฐ, ์ค‘๋ณต๋œ ํ–‰๋„ ๋ชจ๋‘ ํฌํ•จํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ์ค‘๋ณต๋œ ํ–‰์„ ์ œ๊ฑฐํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.


ํŠน์ง•

  • ์ค‘๋ณต ํฌํ•จ: ๋™์ผํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์—ฌ๋Ÿฌ ๋ฒˆ ์„ ํƒํ•œ ๊ฒฝ์šฐ, ๋ชจ๋“  ํ–‰์ด ๊ฒฐ๊ณผ์— ํฌํ•จ๋ฉ๋‹ˆ๋‹ค.
  • ์ •๋ ฌ ์—†์Œ: Union All์€ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— Union๋ณด๋‹ค ๋น ๋ฅด๊ฒŒ ์ˆ˜ํ–‰๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
SELECT `city` FROM `customers`
UNION ALL
SELECT `city` FROM `suppliers`;


๊ฒฐ๊ณผ

city
New York
Los Angeles
New York
Chicago
New York
Chicago
Boston
San Francisco

3. UNION๊ณผ UNION ALL์˜ ์ฐจ์ด์ 

  • UNION: ์ค‘๋ณต๋œ ํ–‰์„ ์ œ๊ฑฐํ•˜๊ณ  ๊ฒฐํ•ฉํ•ฉ๋‹ˆ๋‹ค.
  • UNION ALL: ์ค‘๋ณต๋œ ํ–‰์„ ํฌํ•จํ•˜์—ฌ ๋ชจ๋“  ๊ฒฐ๊ณผ๋ฅผ ๊ฒฐํ•ฉํ•ฉ๋‹ˆ๋‹ค.

4. ์„ฑ๋Šฅ ๊ณ ๋ ค์‚ฌํ•ญ

Union All์€ ์ค‘๋ณต ์ œ๊ฑฐ๋ฅผ ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์ผ๋ฐ˜์ ์œผ๋กœ Union๋ณด๋‹ค ์„ฑ๋Šฅ์ด ๋” ์ข‹์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ, ์ค‘๋ณต๋œ ๋ฐ์ดํ„ฐ๋ฅผ ํฌํ•จํ•ด๋„ ๋ฌธ์ œ๊ฐ€ ์—†๋‹ค๋ฉด Union All์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋” ํšจ์œจ์ ์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

UNION ํ™œ์šฉ ์˜ˆ์ œ๐Ÿ“ˆ

์˜ˆ์ œ ๋ฐ์ดํ„ฐ

์•„๋ž˜๋Š” ํ™œ์šฉ ์˜ˆ์ œ์—์„œ ์‚ฌ์šฉํ•  ์˜ˆ์ œ ๋ฐ์ดํ„ฐ์ž…๋‹ˆ๋‹ค.


employees ํ…Œ์ด๋ธ”

id name age city
1 Alice 35 1
2 Bob 28 2
3 Charlie 40 1


contractors ํ…Œ์ด๋ธ”

id name age city
1 Dave 45 2
2 Eve 29 2
3 Frank 50 3


departments ํ…Œ์ด๋ธ”

id department_name
1 HR
2 IT
3 Finance

๋ณต์žกํ•œ ํ€ด๋ฆฌ์—์„œ์˜ UNION ํ™œ์šฉ ์˜ˆ์ œ

1. ์„œ๋ธŒ์ฟผ๋ฆฌ์™€ UNION ๊ฒฐํ•ฉ

์„œ๋ธŒ์ฟผ๋ฆฌ์™€ UNION์„ ๊ฒฐํ•ฉํ•˜์—ฌ ๋”์šฑ ๋ณต์žกํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”์ถœํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

SELECT `id`, `name` FROM (SELECT `id`, `name` FROM `employees` WHERE `age` > 30)
UNION
SELECT `id`, `name` FROM (SELECT `id`, `name` FROM `contractors` WHERE `age` > 30);

์ด ์ฟผ๋ฆฌ๋Š” employees์™€ contractors ํ…Œ์ด๋ธ”์—์„œ ๋‚˜์ด๊ฐ€ 30์‚ด ์ด์ƒ์ธ ์‚ฌ๋žŒ๋“ค์˜ id์™€ name์„ ์„ ํƒํ•˜๊ณ , ์ด๋ฅผ ํ•˜๋‚˜์˜ ๊ฒฐ๊ณผ ์ง‘ํ•ฉ์œผ๋กœ ๊ฒฐํ•ฉํ•ฉ๋‹ˆ๋‹ค.


๊ฒฐ๊ณผ

id name
1 Alice
3 Charlie
1 Dave
3 Frank

2. ์„œ๋ธŒ์ฟผ๋ฆฌ์™€ UNION ๊ฒฐํ•ฉ

์—ฌ๋Ÿฌ ํ…Œ์ด๋ธ”์„ ์กฐ์ธํ•œ ํ›„ UNION์„ ์‚ฌ์šฉํ•˜๋Š” ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค.

SELECT `e`.`name`, `d`.`department_name` 
FROM `employees` AS `e` 
JOIN `departments` AS `d` ON `e`.`department_id` = `d`.`id`
UNION
SELECT `c`.`name`, `d`.`department_name` 
FROM `contractors` AS `c` 
JOIN `departments` AS `d` ON `c`.`department_id` = `d`.`id`;

employees์™€ contractors ํ…Œ์ด๋ธ”์—์„œ ๊ฐ๊ฐ ๋ถ€์„œ ์ •๋ณด๋ฅผ ๊ฒฐํ•ฉํ•˜์—ฌ ์ด๋ฆ„๊ณผ ๋ถ€์„œ๋ช…์„ ํ•˜๋‚˜์˜ ๊ฒฐ๊ณผ ์ง‘ํ•ฉ์œผ๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. UNION์„ ์‚ฌ์šฉํ•˜์—ฌ ๋‘ ํ…Œ์ด๋ธ”์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฒฐํ•ฉํ•ฉ๋‹ˆ๋‹ค.


๊ฒฐ๊ณผ

name department_name
Alice HR
Charlie HR
Bob IT
Dave IT
Eve HR
Frank Finance

์ฃผ์˜ํ•  ์ ๊ณผ ์ตœ์ ํ™” ๋ฐฉ๋ฒ•โš ๏ธ

์ฃผ์˜ํ•  ์ 

  • SELECT ๋ฌธ์€ ๋ฐ˜๋“œ์‹œ ๊ด„ํ˜ธ๋กœ ๊ฐ์‹ธ์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  • ๋™์ผํ•œ ์นผ๋Ÿผ ์ˆ˜์™€ ๋ฐ์ดํ„ฐ ํƒ€์ž…: ๊ฐ SELECT ๋ฌธ์€ ๋™์ผํ•œ ์ปฌ๋Ÿผ ์ˆ˜์™€ ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ๊ฐ€์ ธ์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  • ์ •๋ ฌ ์ˆœ์„œ: Union ๊ฒฐ๊ณผ์˜ ์ •๋ ฌ ์ˆœ์„œ๋ฅผ ๋ณด์žฅํ•˜๋ ค๋ฉด ORDER BY๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์ตœ์ ํ™” ๋ฐฉ๋ฒ•

  • ์ธ๋ฑ์Šค ํ™œ์šฉ: Union ์—ฐ์‚ฐ์— ์‚ฌ์šฉ๋˜๋Š” ํ…Œ์ด๋ธ”์— ์ ์ ˆํ•œ ์ธ๋ฑ์Šค๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ์„ฑ๋Šฅ์„ ํ–ฅ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋งŒ ์„ ํƒ: SELECT ๋ฌธ์—์„œ ํ•„์š”ํ•œ ์นผ๋Ÿผ๋งŒ ์„ ํƒํ•˜์—ฌ ๋ถˆํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ์‹œ๊ฐ„์„ ์ค„์ž…๋‹ˆ๋‹ค.

ํ•ต์‹ฌ ๋‚ด์šฉ๐Ÿ‘€

MariaDB UNION

728x90
๋ฐ˜์‘ํ˜•