본문 바로가기
공부,일/SQL

20210601_2

by fromnothing1 2021. 6. 1.

JOIN

 

1. INNER JOIN

 

use sqlDB;
select * from userTbl
where userID = 'JYP';
select * from buyTbl
where userID = 'JYP';

select * 
from userTbl
	INNER JoiN buyTbl
	ON userTbl.userID = buyTbl.userID
where buyTbl.userID = 'JYP';

 

self join 실습 

--use sqlDB
--create table smartTbl(
--	num int,
--	emp nchar(3), 
--	manager int, -- 관리자의 사원번호 
--	department nchar(3)
--);

--INSERT INTO smartTbl VALUES(1,'나사장',NULL, NULL);
--INSERT INTO smartTbl VALUES(2,'김재무',1,'재무부');
--INSERT INTO smartTbl VALUES(3,'김부장',2,'재무부');
--INSERT INTO smartTbl VALUES(4,'이부장',2,'재무부');
--INSERT INTO smartTbl VALUES(5,'우대리',4,'재무부');
--INSERT INTO smartTbl VALUES(6,'지사원',4,'재무부');
--INSERT INTO smartTbl VALUES(7,'이영업',1,'영업부');
--INSERT INTO smartTbl VALUES(8,'한과장',7,'영업부');
--INSERT INTO smartTbl VALUES(9,'최정보',1,'정보부');
--INSERT INTO smartTbl VALUES(10,'윤차장',9,'정보부');
--INSERT INTO smartTbl VALUES(11,'이주임',10,'정보부');

select * from smartTbl;


select a.emp as 이름, b.emp as 관리자, b.department as 직속상관부서
from smartTbl a left outer join smartTbl b 
	on a.manager = b.num;

 

 

 

 

'공부,일 > SQL' 카테고리의 다른 글

cursor 사용하기  (0) 2021.07.11
SET NOCOUNT ON  (0) 2021.07.11
20210601  (0) 2021.06.01
210531_2  (0) 2021.05.31
210531  (0) 2021.05.31

댓글