개발로드

[MySQL]JOIN 기초 연습 예제_2 본문

SQL

[MySQL]JOIN 기초 연습 예제_2

위대한개발자 2024. 2. 13. 16:38

1)  pordertbl과  producttbl테이블을 이용해서 orderDate, pcode, 

-- pname,  amount,  판매가격( amount * price)을 조회


select orderDate, od.pcode, pname, amount, format((amount*price),0) as 판매가격
from pordertbl as od join producttbl as pd
on od.pcode = pd.pcode;



2) pordertbl 테이블과  customertbl 테이블을 이용
-- 주문자(cname), 주문일자(orderdate), 개수(amount),  주소(address)를 조회

 


select cname as 주문자, orderdate as 주문일자, amount as 개수, address as 주소 
from pordertbl as od join customertbl as ct
on od.custid = ct.custid;



3) pordertbl 테이블과  customertbl 테이블을 이용
-- cname 이 '김태연'인 사람이 주문한 제품을 검색 
-- 주문자(cname), 주문일자(orderdate), 개수(amount),  주소(address)를 조회


select cname as 주문자, orderdate as 주문일자, amount as 개수, address as 주소
from pordertbl as od join customertbl as ct
on od.custid = ct.custid
where cname = '김태연';



4) pordertbl 테이블,  cutomertbl 테이블, producttbl 테이블을 모두 이용
-- 주문자(cname), 주문일자(orderdate), 상품명(pname), 주문수량(amount), 주문금액(amount *  price)
-- 주문수량이 30개이상인 자료만 주문일자별로 오름차순 정렬하여 조회


select cname as 주문자, orderdate as 주문일자, pname as 상품명, amount as 주문수량, format((amount*price),0) as 주문금액
from pordertbl as ot join customertbl as ct 
on ot.custid = ct.custid
join producttbl as pt
on ot.pcode = pt.pcode
where amount >= 30
order by orderdate asc;

 



5) pordertbl  테이블과 customertbl테이블을 이용하여
-- left outer join과 right outer join을 비교


1) left outer join 


select * from pordertbl as pt left outer join customertbl as ct on pt.custid=ct.custid;


2) right outer join


select * from pordertbl as pt right outer join customertbl as ct on pt.custid = ct.custid;


마치며

MySQL의 Join의 간단한 예제를 풀어보았습니다.

 

여유가 되신다면 제 GitHub에 오셔서 좋은 코드들을 구경해주세요!

 

https://github.com/gimpo5975?tab=repositories

 

gimpo5975 - Overview

gimpo5975 has 2 repositories available. Follow their code on GitHub.

github.com