⬛ 조인 (join)
→ 한 개 이상의 테이블과 테이블을 결합하여 사용.
→ 보통 SELECT 문과 함께 자주 사용됨.
→ orders 테이블과 customers 테이블로 예를 들수있다.
◼ 상호 조인 (CROSS JOIN)
→ 양 테이블 모든 행들을 조인시키는 기능.
→ customers와 orders 테이블의 데이터 개수가 각각 5개였기 때문에,
두 테이블 결합시 25개의 데이터가 조회된다.
SELECT * FROM customers, orders;
◼ 내부 조인 (INNER JOIN)
→ 두 개 이상의 테이블의 교집합에서
데이터를 확인하고자 할 때 사용.
→ ON 절과 함께 사용되며,
ON 절의 조건을 만족하는 데이터만 가져옴.
SELECT * FROM customers
INNER JOIN orders
ON customers.id = orders.customer_id;
→ orders 테이블의 customer_id 와 customers 테이블의 id가 일치하는 데이터만 조회된다.
◼ LEFT JOIN
→ 왼쪽 테이블의 모든 데이터와 오른쪽 테이블의
일치하는 데이터를 조회한다.
SELECT * FROM orders
LEFT JOIN customers
ON orders.customer_id = customers.id;
→ orders 테이블을 왼쪽 테이블로 기준을 잡고 조회한 결과.
→ orders 테이블의 customer_id값과 customers 테이블의 id값이 일치하는 데이터가 조회된다.
◼ RIGHT JOIN
→ LEFT 조인과 반대이다.
→ 일치하지 않는 값은 NULL 값으로 조회된다.
SELECT * FROM orders
RIGHT JOIN customers
ON orders.customer_id = customers.id;
아래 홈페이지 참조.
https://www.w3schools.com/sql/sql_join.asp
SQL Joins
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
www.w3schools.com
'MySQL' 카테고리의 다른 글
MySQL 서브쿼리(Subquery) (0) | 2022.01.21 |
---|---|
primaryKey, foreignKey (0) | 2022.01.20 |
MySQL 논리 연산자 and 비교 연산자 (0) | 2022.01.17 |
MySQL 날짜, 시간 타입(DATE and TIME) (0) | 2022.01.17 |
MySQL 숫자 타입(numeric types) (0) | 2022.01.15 |
댓글