[MySQL] JOIN : CROSS JOIN, INNER JOIN, LEFT JOIN, RIGHT JOIN / UNION


create table customers(
	customerid		char(10)	primary key,
    name			char(10)	not null,
    email			char(20)
);

create table orders(
	num				int			primary key	auto_increment,
    orderdate		char(15)	not null,
    customerid		char(10),
    amount			int			not null
);

insert into customers 
values
('A123', 'Alice', 'aceee@email.com'),
('j_mes_id', 'James', 'j_mes@email.com'),
('popo', 'Pauline', 'pol2@email.com');

insert into orders
(orderdate, customerid, amount)
values
('21-10-25', 'j_mes_id', 56000),
('21-11-17', 'popo', 73200),
('21-11-27', 'j_mes_id', 34900);



JOIN : ν…Œμ΄λΈ”μ˜ κ²°ν•©




CROSS JOIN


  • ν•œ μͺ½ ν…Œμ΄λΈ”μ˜ λͺ¨λ“  ν–‰λ“€κ³Ό λ‹€λ₯Έ ν…Œμ΄λΈ”μ˜ λͺ¨λ“  행을 μ‘°μΈμ‹œν‚€λŠ” κΈ°λŠ₯

  • SELECT * FROM table_name1, table_name2; 
    
  • ex)

    SELECT * FROM customers, orders;
    



INNER JOIN


  • κΈ°μ€€ ν…Œμ΄λΈ”κ³Ό 쑰인 ν…Œμ΄λΈ” λͺ¨λ‘μ— 데이터가 μ‘΄μž¬ν•΄μ•Ό 쑰회됨

  • IMPLICIT INNER JOIN

    • SELECT * FROM table_name1, table_name2
           WHERE table_name1.column_name = table_name2.column_name;
      
    • ex)

        select * from customers, orders where customers.customerid = orders.customerid order by num;
      

  • EXPLICIT INNER JOIN

    • SELECT * FROM table_name1
      JOIN table_name2
      ON table_name1.column_name = table_name2.column_name;
      
    • ex)

        select * from customers
        join orders
        on customers.customerid = orders.customerid;
      



LEFT JOIN


  • ν…Œμ΄λΈ”1 전체 + ν…Œμ΄λΈ”1, ν…Œμ΄λΈ”2의 합집합

  • μΌμΉ˜ν•˜λŠ” 값이 없더라도 μ™Όμͺ½ κ°’(ν…Œμ΄λΈ”1의 κ°’) λͺ¨λ‘ 쑰회, 데이터가 μ—†μœΌλ©΄ NULL

  • SELECT * FROM table_name1
    LEFT JOIN table_name2
    ON table_name1.column_name = table_name2.column_name;
    
  • ex)

      SELECT * FROM customers
      LEFT JOIN orders
      ON customers.customerid = orders.customerid order by num;
    



RIGHT JOIN


  • ν…Œμ΄λΈ”1, ν…Œμ΄λΈ”2의 합집합 + ν…Œμ΄λΈ”2 전체

  • μΌμΉ˜ν•˜λŠ” 값이 없더라도 였λ₯Έμͺ½ κ°’(ν…Œμ΄λΈ”2의 κ°’) λͺ¨λ‘ 쑰회, 데이터가 μ—†μœΌλ©΄ NULL

  • SELECT * FROM table_name1
    RIGHT JOIN table_name2
    ON table_name1.column_name = table_name2.column_name;
    
  • ex)

      insert into orders
      (orderdate, customerid, amount)
      values
      ('21-11-28', 'mylll', 46300);
    
      SELECT * FROM customers
      RIGHT JOIN orders
      ON customers.customerid = orders.customerid order by num;
    



UNION


  • JOIN : κ°€λ‘œ κ²°ν•© 방식 / UNION : μ„Έλ‘œ κ²°ν•© 방식

  • SELECT column_name1 FROM table_name1
    UNION (ALL)
    SELECT column_name2 FROM table_name2
    
    • 두 개 ν…Œμ΄λΈ”μ˜ μ‘°νšŒν•  컬럼 κ°œμˆ˜λŠ” μΌμΉ˜ν•΄μ•Ό 함

    • UNION ALL : μ€‘λ³΅λœ 데이터 λͺ¨λ‘ ν‘œμ‹œ

  • ex)

      create table schoolA(
          name		char(10),
          subject	char(10),
          score		int
      );
    
      insert into schoolA
      values
      ('μ΅œκ°•', 'κ΅­μ–΄', 86),
      ('κ°•μƒˆλ²½', 'μ˜μ–΄', 78);
    
    
      create table schoolB(
          stuName		char(10),
          stuSubject	char(10),
          stuScore	int
      );
    
      insert into schoolB
      values
      ('κ°•μƒˆλ²½', 'μ˜μ–΄', 78),
      ('λ°•ν•˜λŠ˜', 'μ˜μ–΄', 89),
      ('이바닀', 'μˆ˜ν•™', 100);
    
      # union
      select name, score from schoolA
      union
      select stuName, stuScore from schoolB;
    
      # union all
      select name, score from schoolA
      union all
      select stuName, stuScore from schoolB;
    

Categories:

MySQL