๐ฅ Join
- ๋ ํ ์ด๋ธ์ ๊ณตํต๋ ์ ๋ณด(key)๋ฅผ ๊ธฐ์ค์ผ๋ก ํ ์ด๋ธ์ ์ฐ๊ฒฐํด์ ํ ํ ์ด๋ธ์ฒ๋ผ ๋ณด๋ ๊ฒ
- key: ๋ ํ ์ด๋ธ ๋ชจ๋ ๊ฐ๊ณ ์๋ ํ๋ → ๋ ํ ์ด๋ธ์ ์ฐ๊ฒฐ์์ผ์ฃผ๋ ์ด์ ๊ฐ ๋๋ค.
- ์๋ก ๋ค๋ฅธ ํ ์ด๋ธ์ ์๋ ๋ฐ์ดํฐ๋ค์ ์ฌ์ฉํด์ผ ํ ๋ ํ ์ด๋ธ๋ค์ ํฉ์ณ์ค๋ค.
๐ฅ Left Join
๊ฐ๋
- ํฉ์งํฉ
- ๋ชจ๋ ๋ฐ์ดํฐ๋ฅผ ํฉ์น๋ค.
- ๋ ํ ์ด๋ธ์ ํฉ์น ๋ keyํ๋์ ๋ฐ์ดํฐ๊ฐ ํ ํ ์ด๋ธ์๋ ์๊ณ ๋ค๋ฅธ ํ ์ด๋ธ์๋ ์์ ์ ์๋ค. left join์ ๋ชจ๋ ๋ฐ์ดํฐ๋ฅผ ํฉ์น๊ธฐ ๋๋ฌธ์, ์ด ๊ฒฝ์ฐ ๋น์์๋ ๊ณต๊ฐ์ด ์๊ธด๋ค.
select * from users u
left join point_users p
on u.user_id = p.user_id;
์ด๋ค ๋ฐ์ดํฐ(ํ ํ)๋ ๋ชจ๋ ํ๋๊ฐ ์ฑ์์ ธ ์์ง๋ง, ์ด๋ค ๋ฐ์ดํฐ๋ [Null]๋ก ๋น์ด์๋ ํ๋๊ฐ ์๋ค.
usersํ ์ด๋ธ๊ณผ point_users ํ ์ด๋ธ์ key๋ user_id์ด๋ค.
๋ชจ๋ ์ ์ ๊ฐ ํฌ์ธํธ๋ฅผ ๊ฐ๊ณ ์์ง ์๊ธฐ ๋๋ฌธ์, users ํ ์ด๋ธ์ ์๋ user_id๊ฐ point_users ํ ์ด๋ธ์๋ ์์ ์ ์๋ค.
๋ฐ๋ผ์ usersํ ์ด๋ธ๊ณผ point_user๋ฅผ left join์ผ๋ก ํฉ์น ๊ฒฐ๊ณผ ๋น ๊ณต๊ฐ์ด ์๊ธด๋ค.
์ฌ์ฉ ์์
- user์ค์ ์ด๋ฆ๋ณ๋ก point๊ฐ ์๋ ์ฌ๋(์์ํ์ง ์์ ์ฌ๋)๋ค ํต๊ณ
select name, count(*) from users u
left join point_users pu on u.user_id = pu.user_id
where pu.point_user_id is NULL
group by name
- 7์ 13์ผ ~ 16์ผ์ ๊ฐ์ ํ ๊ณ ๊ฐ ์ค, ํฌ์ธํธ๋ฅผ ๊ฐ์ง ๊ณ ๊ฐ์ ์ซ์, ์ ์ฒด์ซ์, ๊ทธ๋ฆฌ๊ณ ๊ทธ ๋น์จ์ ๋ณด๊ธฐ
select count(p.point_user_id) as pnt_user_cnt,
count(*) as tot_user_cnt,
round(count(p.point_user_id)/count(*),2) as ratio
from users u
left join point_users p on u.user_id = p.user_id
where u.created_at between '2020-07-13' and '2020-07-17'
๐ฅ Inner Join
๊ฐ๋
- ๊ต์งํฉ
- ๋ ํ ์ด๋ธ์ ํฉ์น ๋ keyํ๋์ ๋ฐ์ดํฐ๊ฐ ํํ ์ด๋ธ์๋ ์๊ณ ๋ค๋ฅธ ํ ์ด๋ธ์๋ ์์ ์ ์๋ค. inner join์ ๋ ํ ์ด๋ธ์์ ๋ชจ๋ ๊ฐ์ง๊ณ ์๋ ๋ฐ์ดํฐ๋ง ์ถ๋ ฅํ๋ค.
select * from users u
inner join point_users p
on u.user_id = p.user_id;
โป ์ฟผ๋ฆฌ ์คํ ์์: from → join → select
1. from users u : usersํ ์ด๋ธ์์ ๋ฐ์ดํฐ ์ ์ฒด๋ฅผ ๊ฐ์ ธ์จ๋ค.
2. inner join point_users p on u.user_id = p.user_id : point_users๋ฅผ usersํ ์ด๋ธ์ ๋ถ์ด๋๋ฐ, usersํ ์ด๋ธ์ user_id์ ๋์ผํ user_id๋ฅผ ๊ฐ๋ point_users์ ํ ์ด๋ธ์ ๋ถ์ธ๋ค.
3. select * : ๋ถ์ฌ์ง ๋ชจ๋ ๋ฐ์ดํฐ๋ฅผ ์ถ๋ ฅํ๋ค.
โป from์ ๋ค์ด๊ฐ ํ ์ด๋ธ ๊ธฐ์ค์ผ๋ก, join ๋ค์ ํ ์ด๋ธ์ด ๋ถ๋๋ค
โป join์ ์คํ ์์๋ ํญ์ from๊ณผ ๋ถ์ด๋ค๋๋ค. (from → join)
์ฌ์ฉ์์
- ๋ค์ด๋ฒ ์ด๋ฉ์ผ์ ์ฌ์ฉํ๋ ์ ์ ์ ์ฑ์จ๋ณ ์ฃผ๋ฌธ๊ฑด์ ์ธ์ด๋ณด๊ธฐ
select u.name, count(u.name) as count_name from orders o
inner join users u on o.user_id = u.user_id
where u.email like '%naver.com'
group by u.name
์ฟผ๋ฆฌ ์คํ ์์: from → join → where → group by → select
- ์น๊ฐ๋ฐ, ์ฑ๊ฐ๋ฐ ์ข ํฉ๋ฐ์ week๋ณ ์ฒดํฌ์ธ ์ ์ธ๊ธฐ
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
โป ์ฟผ๋ฆฌ ์คํ ์์: from → join → group by → order by → select
1. from checkins c: checkins ํ ์ด๋ธ ๋ฐ์ดํฐ ์ ์ฒด๋ฅผ ๊ฐ์ ธ์จ๋ค.
2. inner join courses c2 on c.course_id = c2.course_id : checkinsํ ์ด๋ธ์ course_id์ ๋์ผํ course_id๋ฅผ ๊ฐ๋ courses ํ ์ด๋ธ์ ๋ถ์ธ๋ค.
3. group by c2.title, c.week : coursesํ ์ด๋ธ์ ๊ฐ์ title๋ณ๋ก(์น,์ฑ๊ฐ๋ฐ ์ข ํฉ๋ฐ), checkinsํ ์ด๋ธ์ ๊ฐ์ week๋ณ๋ก ๋ฐ์ดํฐ๋ฅผ ํฉ์ณ์ค๋ค.
4. select c2.title, c.week, count(*) : (title), (week), (title, week๋ณ๋ก ๊ฐ๊ฐ ๋ช๊ฐ๊ฐ ํฉ์ณ์ง ๊ฒ์ธ์ง ์ธ์ด์ค ํ๋)๋ฅผ ์ถ๋ ฅํ๋ค.
๐ฅ Union
- select ๋๊ฐ๋ก ์ด๋ฃจ์ด์ง ํ ์ด๋ธ์ ํฉ์น๋ค.
select '7์' as month, c.title, c2.week, count(*) as cnt from checkins c2
inner join courses c on c2.course_id = c.course_id
inner join orders o on o.user_id = c2.user_id
where o.created_at < '2020-08-01'
group by c2.course_id, c2.week
order by c2.course_id, c2.week
select '8์' as month, c.title, c2.week, count(*) as cnt from checkins c2
inner join courses c on c2.course_id = c.course_id
inner join orders o on o.user_id = c2.user_id
where o.created_at > '2020-08-01'
group by c2.course_id, c2.week
order by c2.course_id, c2.week
(
select '7์' as month, c.title, c2.week, count(*) as cnt from checkins c2
inner join courses c on c2.course_id = c.course_id
inner join orders o on o.user_id = c2.user_id
where o.created_at < '2020-08-01'
group by c2.course_id, c2.week
order by c2.course_id, c2.week
)
union all
(
select '8์' as month, c.title, c2.week, count(*) as cnt from checkins c2
inner join courses c on c2.course_id = c.course_id
inner join orders o on o.user_id = c2.user_id
where o.created_at > '2020-08-01'
group by c2.course_id, c2.week
order by c2.course_id, c2.week
)
ํ์ง๋ง union์์ ๋ด๋ถ ์ ๋ ฌ์ด ์ ๋จน๋๋ค.
SubQuery๋ฅผ ์ฌ์ฉํ์!
Source
์คํ๋ฅดํ ์ฝ๋ฉํด๋ฝ ๋ด์ผ๋ฐฐ์๋จ ์์ ๋ณด๋จ ์ฌ์ด SQL 3์ฃผ์ฐจ
'Database > SQL' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[MySQL] ๋ฐ์ดํฐ ๋ฒ ์ด์ค ์ธ๋ฑ์ค (0) | 2022.10.08 |
---|---|
[MySQL] DB, Table, Data, Column ์์ฑ, ์กฐํ, ์์ , ์ญ์ (0) | 2022.09.26 |
[MySQL] [์คํ๋ฅดํ์ฝ๋ฉํด๋ฝ SQL] Subquery, With, ๋ฌธ์์ด, Case (0) | 2022.07.16 |
[MySQL] [์คํ๋ฅดํ์ฝ๋ฉํด๋ฝ SQL] Group by, Order by (0) | 2022.07.09 |
[MySQL] [์คํ๋ฅดํ์ฝ๋ฉํด๋ฝ SQL] Select, Where (0) | 2022.07.01 |