join
μ΄λ ?λ ν
μ΄λΈμ 곡ν΅λ μ 보(key)λ₯Ό κΈ°μ€μΌλ‘ ν
μ΄λΈμ μ°κ²°ν΄μ ν ν
μ΄λΈμ²λΌ λ³Ό μ μλλ‘ νλ κ²
π μνλ μ λ³΄κ° Aμλ μκ³ Bμλ μμ λ μλ‘ λ¬Άμ΄μ νμΈν μ μλ€.
left join
'AλΌλ ν μ΄λΈκ³Ό BλΌλ ν μ΄λΈμ 곡ν΅λ μ 보(key)λ₯Ό μ°κ²°νμ¬ AλΌλ ν μ΄λΈμ BλΌλ ν μ΄λΈμ λΆμΈλ€'λ λλ(?)π‘
μ΄λ€ λ°μ΄ν°λ λͺ¨λ νλκ° μ±μμ Έμμ§λ§, μ΄λ€ λ°μ΄ν°λ λͺ¨λ νλκ° μ±μμ Έμμ§ μκ³ λΉμ΄μλ νλκ° μ‘΄μ¬ν λ, μ μμ μΌλ‘ λ°μ΄ν°κ° μΆλ ₯λμ§ μμ μ μλ€.
select νλλͺ
from ν
μ΄λΈλͺ
(μΌμͺ½)
left join μΌμͺ½ν
μ΄λΈμ λΆμΌ λ€λ₯Έ ν
μ΄λΈ
on μΌμͺ½ν
μ΄λΈμ νλ = μ€λ₯Έμͺ½ν
μ΄λΈμ νλ // 곡ν΅λ μ 보
select * from A_table a
left join B_table b
on a.number = b.number
inner join
'AλΌλ ν μ΄λΈκ³Ό BλΌλ ν μ΄λΈμ 곡ν΅λ μ 보(key)λ§ λ¬Άμ κ΅μ§ν© λλ(?)π‘
inner join
μ μ¬μ©νκ² λλ©΄ NULL (λΉμ΄μλ νλκ° μλ λ°μ΄ν°) μ΄ μλ€ !
inner join
μ κ΅μ§ν©μ΄μ΄μ Aμ B λͺ¨λ ν μ΄λΈμ΄ κ°μ§κ³ μλ λ°μ΄ν°λ₯Ό λ¬Άκ³ ,left join
λ λ¬Όλ‘ κ³΅ν΅λ λ°μ΄ν°λ μμ§λ§ νμͺ½μλ§ μλ λ°μ΄ν°κ° μμ΄ NULL μ΄ μ‘΄μ¬ν μ μλ€.
select νλλͺ
from ν
μ΄λΈλͺ
(μΌμͺ½)
inner join μΌμͺ½ν
μ΄λΈμ λΆμΌ λ€λ₯Έ ν
μ΄λΈ
on μΌμͺ½ν
μ΄λΈμ νλ = μ€λ₯Έμͺ½ν
μ΄λΈμ νλ // 곡ν΅λ μ 보
select * from A_table a
inner join B_table b
on a.number = b.number
union
SELECT
λ₯Ό λλ²νμ§ μκ³ ν λ²μ λͺ¨μμ λ³΄κ³ μΆμ κ²½μ°μ μ¬μ©νλ€.
ex) 11μ λ°μ΄ν°μ 12μ λ°μ΄ν°λ₯Ό ν λ²μ λ³΄κ³ μΆμ λ.
(
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 < '2012-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 > '2012-08-01'
group by c2.course_id, c2.week
order by c2.course_id, c2.week
)