본문 바로가기

Engineering/DB

SQL LEFT JOIN 간단 예제

728x90

SQL LEFT JOIN 간단 예제

: MySQL 에서 테스트.


drop table A;

drop table B;


create table A(id int, val int);

create table B(id int, val int);


INSERT INTO A VALUES(1, 10);

INSERT INTO A VALUES(2, 20);

INSERT INTO A VALUES(3, 30);


INSERT INTO B VALUES(1, 100);

INSERT INTO B VALUES(2, 200);

INSERT INTO B VALUES(3, 300);


-- SELECT * FROM A;

-- SELECT * FROM B;



select a.*,b.*

from   A a left join B b 

-- from   B b left join A a 

on     a.id =b.id and a.id=2;

-- on     a.id =b.id;


실행 결과

id  val  id      val     

--  ---  ------  ------  

2   20   2       200     

1   10   (null)  (null)  

3   30   (null)  (null)