- SQL query JOIN
demo_people 테이블
| name | phone | pid |
|---|---|---|
| Mr Brown | 01225 708225 | 1 |
| Miss Smith | 01225 899360 | 2 |
| Mr Pullen | 01380 724040 | 3 |
demo_property 테이블
| pid | spid | selling |
|---|---|---|
| 1 | 1 | Old Houser Farm |
| 3 | 2 | The Willows |
| 3 | 3 | Tall Trees |
| 3 | 4 | The Melksham Florist |
| 4 | 5 | Dun Roamin |
pid가 같은 것을 조건으로
xxxxxxxxxxselect name, phone, sellingfrom demo_people join demo_propertyon demo_people.pid = demo_property.pid;
pid가 서로 같은이라는 조건을 두고
demo_people 과 demo_property을 join하여
name, phone, selling 을 불러왔다.
| name | phone | selling |
|---|---|---|
| Mr Brown | 01225 708225 | Old House Farm |
| Miss Smith | 01225 899360 | NULL |
| Mr Pullen | 01380 724040 | The Willows |
| Mr Pullen | 01380 724040 | Tall Trees |
| Mr Pullen | 01380 724040 | The Melksham Florist |
이때, 서로 같은 것이 존재하지 않는 줄은 아예 출력되지 않는 것을 볼 수 있다.
예) Miss Smith
right join 혹은 left join을 통해 반드시 출력되는 테이블을 잡아 줄 수 있다
xxxxxxxxxxselect name, phone, sellingfrom demo_people left join demo_propertyon demo_people.pid = demo_property.pid;| name | phone | selling |
|---|---|---|
| Mr Brown | ~~ | Old House Farm |
| Miss Smith | ~~ | NULL |
| Mr Pullen | ~~ | The Willows |
| Mr Pullen | ~~ | Tall Tress |
| Mr Pullen | ~~ | The Melksham Florist |
left join을 사용했기에 왼쪽테이블(demo_people)이 기준이 된다.
따라서 왼쪽 테이블의 모든 Row가 반드시 한 줄 이상 나오는 보장을 받는다.
right join같은 경우도 위와 같다.
xxxxxxxxxxINNER JOIN 은 JOINLEFT OUTER JOIN 은 LEFT JOINRIGHT OUTER JOIN은 RIGHT JOIN 과 같다
'Back-end' 카테고리의 다른 글
| Elastic Search 벌크 (0) | 2018.08.29 |
|---|---|
| Elastic Search 자료구조 / GET POST PUT DELETE / UPDATE방법 (0) | 2018.08.29 |
| Flask 익히기 (0) | 2018.07.07 |
| Django Apache2 연동 (0) | 2018.04.25 |
| Python uwsgi로 서버 배포 (0) | 2018.04.23 |
댓글