티스토리 뷰

<Join 이란...>

두 테이블의 공통된 필드(key 값)를 기준으로 테이블을 연결해서 한 테이블처럼 보는 것을 의미한다.

Join의 종류에는 Left Join, Inner Join이 있다.

Left Join은 A 테이블의 왼쪽에 B 테이블을 붙이는 것이다. 이때 해당 데이터의 필드값이 비어있다면 [Null]로 표시되어 출력된다.(A와 B 사이의 크기를 따져야한다.)

Inner Join은 두 테이블의 공통된 데이터값만을 출력한다.

 

<사용방법>

select * form table을 해보면서 필드값을 확인하고 두 테이블의 공통된 필드값을 찾는다.

이후,(별칭 사용)

select * from users u
inner join point_users p 

on u.user_id = p.user_id

 

<쿼리 실행 순서>

from --> join --> where --> group by --> select

  1. from users u: 테이블 데이터 전체를 가져옵니다.
  2. inner join point_users p on u.user_id = p.user_id: users에 point_users를 갖다 붙이는데, users 테이블의 user_id 필드와 동일한 user_id를 갖는 point_users의 테이블을 붙입니다.
  3. select * : 붙여진 모든 데이터를 출력합니다.
  • from에 들어간 테이블을 기준으로, 다른 테이블이 붙는다고 생각하자.

 

<연습 예제>

-- 3주차 3-1 ~ -------------------------------------------------------------------------
select * from users u 

select * from point_users 

# left join(합집합): 값이 없는 칸은 [null]로 표시
select * from users u
left join point_users p on u.user_id = p.user_id

# inner join(교집합): 값이 들어있는 줄만 출력
select * from users u
inner join point_users p on u.user_id = p.user_id

# orders 테이블에 users 테이블 연결해보기
select * from orders o 

select * from users u 

select * from orders o 
inner join users u on o.user_id = u.user_id 

# checkins 테이블에 users 테이블 연결해보기
select * from checkins c 

select * from users u 

select * from checkins c 
inner join users u on c.user_id = u.user_id 

# enrolleds 테이블에 courses 테이블 연결해보기
select * from enrolleds e 

select * from courses c 

select * from enrolleds e 
inner join courses c on e.course_id = c.course_id 

# '오늘의 다짐' 정보에 과목 정보를 연결해 과목별 '오늘의 다짐' 갯수를 세어보자
select * from checkins

select * from courses

select c.course_id, c2.title, count(*) as cnt from checkins c 
inner join courses c2 on c.course_id = c2.course_id 
group by c.course_id

# 유저의 포인트 정보가 담긴 테이블에 유저 정보를 연결해서, 많은 포인트를 얻은 순서대로 유저의 데이터를 뽑아보자(이름, 이메일, 포인트)
select * from point_users

select * from users

select u.name, u.email, pu.point from users u 
inner join point_users pu on u.user_id = pu.user_id
order by pu.point desc

# 주문 정보에 유저 정보를 연결해서 네이버 이메일을 사용하는 유저 중, 성씨별 주문건수를 세어보자.
select * from orders

select * from users 

select u.name, count(*) as '주문건수' from orders o 
inner join users u on o.user_id = u.user_id 
where o.email like '%naver.com'
group by u.name

# 결제 수단 별 유저 포인트의 평균값 구해보기(반올림)
select * from orders

select * from point_users

select o.payment_method, round(avg(pu.point),2) from orders o 
inner join point_users pu on o.user_id = pu.user_id 
group by o.payment_method

# 결제하고 시작하지 않은 유저들을 성씨별로 세어보기
select * from enrolleds

select * from users

select u.name, count(*) from users u 
inner join enrolleds e on u.user_id = e.user_id
where e.is_registered = 0
group by u.name
order by count(*) desc

# 과목별로 시작하지 않은 유저들을 세어보기
select * from enrolleds

select * from courses

select c.title, count(*) as '미시작' from enrolleds e 
inner join courses c on e.course_id = c.course_id 
where e.is_registered = 0
group by c.title

# 웹개발, 앱개발 종합반의 week 별 체크인 수를 세어보기
select * from checkins

select * from courses

select c2.title, c.week, count(*) from checkins c 
inner join courses c2 on c.course_id = c2.course_id 
group by c2.title, c.week
order by c2.title, c.week

# 위 조건에서, 8월 1일 이후에 구매한 고객들을 추려보자
select * from orders

select c2.title, c.week, count(*) as '8월 1일 이후' from checkins c 
inner join courses c2 on c.course_id = c2.course_id
inner join orders o on c.user_id = o.user_id
where o.created_at >= '2020-08-01'
group by c2.title, c.week
order by c2.title, c.week 

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG more
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함