여러 테이블 한번에 다루기

오상윤·2023년 1월 30일
0

SQL

목록 보기
8/10

여러 테이블 한번에 다루기 (집합 연산)

  • 합집합, 교집합, 차집합

데이터에 데이터 더하기 (UNION, UNION ALL)

UNION, UNION ALL

  • 합집합
  • [쿼리 A] UNION [쿼리 B] 또는 [쿼리 A] UNION ALL [쿼리 B] 형식으로 사용합니다.
  • [쿼리 A] 와 [쿼리 B]의 결과 값을 합쳐서 보여줍니다
  • UNION은 동일한 값은 제외하고 보여주며, UNION ALL은 동일한 값도 포함하여 보여줍니다.
    - [쿼리 A]와 [쿼리 B]의 결과 값의 개수가 같아야 한다
    - 만약 다를 경우 에러 발생
    - ORDER BY는 쿼리 가장 마지막에 작성 가능하고, [쿼리 A]에서 가져온 컬럼으로만 가능합니다.
    SELECT [컬럼이름]
    FROM [테이블 A 이름]
    UNION (ALL)
    SELECT [컬럼 이름]
    FROM [테이블 B 이름];

데이터에서 데이터 빼기 (교집합, 차집합)

교집합

  • MySQL에는 문법이 존재하지 않아 JOIN으로 한다
    SELECT [컬럼 이름]
    FROM [테이블 A 이름] AS A
    INNER JOIN [테이블 B 이름] AS B
    ON A.[컬럼1 이름] = B.[컬럼1 이름] AND ... AND A.[컬럼N 이름] = B.[컬럼N 이름]

차집합

SELECT [컬럼 이름]
FROM [테이블 A 이름] AS A
LEFT JOIN [테이블 B 이름] AS B
ON A.[컬럼1 이름] = B.[컬럼1 이름] AND ... AND A.[컬럼N 이름] = B.[컬럼N 이름]
WHERE B.[컬럼 이름] IS NULL;

실습1

DROP DATABASE IF EXISTS pokemon;
CREATE DATABASE pokemon;
USE pokemon;
CREATE TABLE mypokemon(
		number int,
        name varchar(20),
        type varchar(20),
        height float,
        weight float,
        attack int,
        defense int,
        );
CREATE TABLE friendpokemon(
		number int,
        name varchar(20),
        type varchar(20),
        height float,
        weight float,
        attack int,
        defense int,
        );        
INSERT INTO mypokemon(number, name, type, attack, defense)
VALUES (10,'caterpie', 'bug', 30, 35),
	(25,'picachu','electric', 55, 40), 
    (27,'raichu','electric', 90, 55),
    (133,'eevee','normal', 55, 50),
    (152,'chikoirita','grass', 49, 65);
INSERT INTO friendpokemon(number, name, type, attack, defense)
VALUES (26,'raichu', 'electric', 80, 60),
	(125,'electabuzz','electric', 83, 57), 
    (137,'porygon','normal', 60, 70),
    (153,'bayleef','grass', 62, 80),
    (172,'pichu','electric', 40, 15),
    (470,'leafeon','grass', 110, 130); 
  1. 내 포켓몬과 친구의 포켓몬에 어떤 타입들이 있는지 중복 제외하고 같은 타입은 한 번 씩만 가져와 주세요.
SELECT distinct type
FROM mypoekmon
UNION
SELECT distinct type
FROM friendpokemon;
  1. 내 포켓몬과 친구의 포켓몬 중에 풀(GRASS)타입 포켓몬들의 포켓몬 번호와 이름을 중복 포함하여 전부 다 가져와 주세요
SELECT number, name
FROM mypokemon
WHERE type = 'grass'
UNION ALL
SELECT number, name
FROM friendpokemon
WHERE type = 'grass';

실습2

DROP DATABASE IF EXISTS pokemon;
CREATE DATABASE pokemon;
USE pokemon;
CREATE TABLE mypokemon(
		number int,
        name varchar(20),
        type varchar(20),
        height float,
        weight float,
        attack int,
        defense int,
        );
CREATE TABLE friendpokemon(
		number int,
        name varchar(20),
        type varchar(20),
        height float,
        weight float,
        attack int,
        defense int,
        );        
INSERT INTO mypokemon(number, name, type, attack, defense)
VALUES (10,'caterpie', 'bug', 30, 35),
	(25,'picachu','electric', 55, 40), 
    (27,'raichu','electric', 90, 55),
    (133,'eevee','normal', 55, 50),
    (152,'chikoirita','grass', 49, 65);
INSERT INTO friendpokemon(number, name, type, attack, defense)
VALUES (26,'raichu', 'electric', 80, 60),
	(125,'electabuzz','electric', 83, 57), 
    (137,'porygon','normal', 60, 70),
    (153,'bayleef','grass', 62, 80),
    (172,'pichu','electric', 40, 15),
    (470,'leafeon','grass', 110, 130); 
  1. 나도 가지고 있고, 친구도 가지고 있는 포켓몬의 이름을 가져와 주세요
SELECT mypokemon.name
FROM mypokemon INNER JOIN friendpokemon
ON mypokemon.number = friendpokemon.number AND mypokemon.name = friendpokemon.name;
  1. 나만 가지고 있고, 친구는 안 가지고 있는 포켓몬의 이름을 가져와 주세요.
  • 내가 가지고 있는 포켓몬 - 친구가 가지고 있는 포켓몬
SELECT mypokemon.name
FROM mypokemon LEFT JOIN friendpokemon
ON mypokemon.number = friendpokemon.number AND mypokemon.name = friendpokemon.name
WHERE friendpokemon.number IS NULL;
profile
가보자가보자~

0개의 댓글