join: 두개 이상의 테이블들을 연결 또는 결합하여 데이터를 출력하는 것
- 1. equal join [교집합]
select 테이블1.컬럼명1, 테이블2.컬럼명
from 테이블1, 테이블2
where 테이블1.컬럼명 = 테이블2.컬럼명
- 2. inner join [교집합]
select 테이블1.컬럼명1, 테이블2.컬럼명
from 테이블1 join 테이블2
on 테이블1.컬럼명 = 테이블2.컬럼명
- 3. outer join [합집합]
1) equal join
방법1) select userinfo.no, userinfo.name, userinfo.age, userban.ban from userinfo, userban
where userinfo.no=userban.no;
방법2) select a.no, a.name, a.age, b.ban from userinfo a, userban b
where a.no = b.no;
동일한 방법임!!
2) inner join
방법1)
select c.no, c.name, c.age, d.ban
from userinfo c join userban d
on c.no = d.no;
방법2)
select c.no, c.name, c.age, d.ban
from userinfo c join userban d
using( no );
inner 예시)
select a.no, a.name, a.age, b.ban
from userinfo a join userban b
on a.no = b.no
1) where age between 20 and 40;
2) where a.age>=20 and a.age<=40;
equal 예시)
select a.no, a.name, a.age, b.ban
from userinfo a, userfban b
where a.no=b.no and a.age>=20 and a.age <=40;
join은 자주 사용된다고 한다.
여러개 table을 한 번에 join을 할 수 있다.
사용방법)
select 컬럼
from 테이블1
join 테이블2 on 조건식
join 테이블3 on 조건식;
사용예제)
방법1:
select a.stdno, a.stdname, a.stdjumin, a.stdjuminmobile, a.stdaddress, a.stdemail, b.subcode, c.subtitle
from student a, sugang b, subject c
where a.stdno=b.stdno
and b.subcode=c.subcode
and c.subtitle='oracle';
방법2:
select a.stdno, stdname, stdjumin, stdjuminmobile, stdaddress, stdemail, b.subcode, c.subtitle
from student a
join sugang b on a.stdno=b.stdno
join subject c on b.subcode=c.subcode
where c.subtitle='oracle';
위에 두 코드 모두 동일한 결과를 같는다.
'배운 내용 정리 > MYSQL' 카테고리의 다른 글
rownum (0) | 2020.03.05 |
---|---|
날짜함수, 카운트, 집계함수 (0) | 2020.01.27 |
[학습정리] 2020.01.14 - outer join, repeat (0) | 2020.01.16 |
[학습정리] 2020.01.10 - having (0) | 2020.01.11 |
[학습정리] 2020.01.09 - case, if, group by (0) | 2020.01.10 |