IT/Mysql

Mysql - JOIN, OUTER JOIN

노마드오브 2018. 7. 23. 22:07
SELECT * 
FROM 주문, 제품
WHERE 주문고객 = 'banana'
AND 주문.`주문제품` AND 제품.`제품번호`;

SELECT O.주문제품, O.주문일자
FROM 고객 AS C, 주문 AS O 
WHERE C.나이 >= 30
AND C.고객아이디 = O.주문고객;

SELECT 고객.`고객이름`, 주문.`주문일자`, 제품.`제품명`
FROM 고객, 주문,제품
WHERE 고객.`고객아이디` = 주문.`주문고객`
AND 주문.`주문제품` = 제품.`제품번호`
AND 고객.`나이` >= 30;

SELECT P.제품명
FROM 고객 AS C, 주문 AS O, 제품 AS P
WHERE C.고객이름 = '고명석'
AND O.주문고객 = C.고객아이디
AND O.주문제품 = P.제품번호;

SELECT C.`고객이름`, C.`직업`, P.`제품명`, P.`단가`, P.`제조업체`
FROM 고객 C, 주문 O, 제품 P
WHERE C.`고객아이디` = O.`주문고객`
AND O.`배송지` = '서울시 마포구'
AND O.`주문제품` = P.`제품번호`;

SELECT C.`고객이름`, C.`나이`, O.`수량`
FROM 고객 C, 주문 O, 제품 P
WHERE C.`고객아이디` = O.`주문고객`
AND P.`제품번호` = O.`주문제품`
AND P.`제품명` = '그냥만두';

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

SELECT E.ENAME, E.DEPTNO, D.DNAME 
FROM EMP E, DEPT D
WHERE E.DEPTNO = D.DEPTNO;

SELECT *
FROM EMP D, DEPT D
WHERE E.DEPTNO = D.DEPTNO;

SELECT E.ENAME, E.COMM, E.SAL, D.LOC
FROM EMP E, DEPT D
WHERE E.DEPTNO = D.DEPTNO
AND E.COMM IS NOT NULL;

SELECT SUM(E.SAL), D.DNAME
FROM EMP E, DEPT D
WHERE E.DEPTNO = D.DEPTNO
AND (D.LOC = 'NEW YORK' OR D.LOC = 'DALLAS')
GROUP BY D.DNAME;

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

SELECT * FROM 제품;
SELECT * FROM 주문;

SELECT * 
FROM 제품 JOIN 주문 
ON 제품.`제품번호` = 주문.`주문제품`;

SELECT * 
FROM 제품 LEFT JOIN 주문 
ON 제품.`제품번호` = 주문.`주문제품`;

SELECT * 
FROM 제품 RIGHT JOIN 주문 
ON 제품.`제품번호` = 주문.`주문제품`;

SELECT E.EMPNO, E.ENAME, E2.ENAME AS MGR_NAME
FROM EMP E JOIN EMP E2
ON E.EMPNO = E2.MGR;

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

SELECT B.SEQ_NO, B.TITLE, B.CONTENT, BC.CODE, BC.CODE_EXP
FROM BOARD B, BOARD_CODE BC
WHERE B.CODE = BC.CODE;

SELECT B.SEQ_NO, B.TITLE, B.CONTENT, BC.CODE, BC.CODE_EXP
FROM BOARD B, BOARD_CODE BC
WHERE B.CODE = BC.CODE 
AND BC.CODE != 'Q';

SELECT B.SEQ_NO, B.TITLE, B.CONTENT, BC.CODE, BC.CODE_EXP
FROM BOARD B, BOARD_CODE BC
WHERE B.CODE = BC.CODE 
AND BC.USE_YN = 'Y';

SELECT B.SEQ_NO, B.TITLE, BF.FILE_SEQ_NO, BF.FILE_NAME, BF.FILE_SIZE
FROM BOARD B, BOARD_FILE BF
WHERE B.SEQ_NO = BF.FILE_SEQ_NO;

SELECT B.SEQ_NO, B.TITLE, BF.FILE_SEQ_NO, BF.FILE_NAME, BF.FILE_SIZE
FROM BOARD B
LEFT OUTER JOIN BOARD_FILE BF
ON B.SEQ_NO = BF.FILE_SEQ_NO;

SELECT B.SEQ_NO, B.TITLE, BF.FILE_SEQ_NO, BF.FILE_NAME, BF.FILE_SIZE, BC.CODE_EXP, BC.USE_YN
FROM BOARD B
LEFT OUTER JOIN BOARD_FILE BF
ON B.SEQ_NO = BF.FILE_SEQ_NO
LEFT OUTER JOIN BOARD_CODE BC
ON B.CODE = BC.CODE;