티스토리 뷰

새싹/새싹데이터

19. 조인 이해하기

키성열 2022. 5. 21. 13:53
반응형

 

#동등조인
select username,saleprice
from customer C, orders O
where c.custid = O.custid;

 

이너조인(동등조인과 같다)

#inner join
select username, saleprice
from customer C
join orders O
on C.custid=O.custid;

ON의 조건을 기준으로 join이라는 명령어를 써준다.

 

 

외부 왼쪽 조인

외부조인

도서를 구매하지 않은 고객을 포함해 고객 이름/전화번호와 주문도서의 판매가격을 출력

select username, orders.saleprice
from customer
left outer join orders
on customer.custid=orders.custid;

오른쪽 외부조인

오른쪽(orders.custid)를 중심으로 조인을 하기 때문에 주문이 있는 사람만 출력됨

select username, orders.saleprice
from customer
right outer join orders
on customer.custid=orders.custid;

 

크로스 조인

 

select customer.username, saleprice,orderdate
from customer
cross join orders
on customer.custid=orders.custid;

하나만 나오는 것이 아니라 박지성, 김연아 등이 여러번 나온다.

일치하는 모든 결과를 보여준다.

 

 

not in과 in 연산을 이용해 minus와 intersect 연산을 할 수 있다.

(MYSQL에서는 minsu와 intersect를 지원하지 않는다.)

 

 

 

실습

유니온 연산

select username,phone
from customer
where address like '%서울%'

UNION

select username,phone
from customer
where custid in (select custid from orders);

 

반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/04   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
글 보관함
반응형