[SQL] ์ข์ ์ฟผ๋ฆฌ ์์ฑ๋ฒ
[SQL] LEFT JOIN์ ํ์ฉํ ๋๋ฝ๋ ๋ฐ์ดํฐ ์ฒ๋ฆฌํ๊ธฐ — dovnaldisn (tistory.com)
[SQL] LEFT JOIN์ ํ์ฉํ ๋๋ฝ๋ ๋ฐ์ดํฐ ์ฒ๋ฆฌํ๊ธฐ
์ ํ๋ฅผ ๋ณด๋ฉด codetype์ fruit์ด ์๊ณ codekey์๋ ๊ณผ์ผ์ ์ข ๋ฅ๊ฐ ์ ํ์๋ค. ๊ทธ๋ฆฌ๊ณ ๊ฐ๊ฐ ๊ณผ์ผ์ ๋ํ ์ข ๋ฅ์ ์ด๋ฆ์ด ๋์์๋ค. ํ์ฌ ํฌ๋๋ 4๊ฐ, ์ฌ๊ณผ๋ 2๊ฐ, ์ค๋ ์ง๋ 3๊ฐ, ๋ฐฐ๋ 0๊ฐ์ธ๋ฐ ํฌ๋, ์ฌ๊ณผ,
dovnaldisn.tistory.com
์ด ๊ธ์ ์ฝ๋ ๋ฆฌ๋ทฐ๋ฅผ ๋ฐ๊ณ ์ฌ์์ฑ์ ํด๋ณธ๋ค.
์์ ํ ์ด๋ธ ๊ตฌ์กฐ
์์ ํ ์ด๋ธ ์์ฑ์ ์ด๋ ๊ฒ ๋๋ค.
insert into fruit (codetype, codekey, koname)
values
('apple','green apple','์์ค๋ฆฌ ์ฌ๊ณผ'),
('apple','red apple','์ฌ๊ณผ'),
('fruit','grape','ํฌ๋'),
('fruit','apple','์ฌ๊ณผ'),
('fruit','orrange','์ค๋ ์ง'),
('fruit','pear','๋ฐฐ'),
('grape','kyoho','๊ฑฐ๋ด'),
('grape','shine muscat','์ค์ธ๋จธ์ค์บฃ'),
('grape','black shpphire','๋ธ๋์ฌํ์ด์ด'),
('grape','green grape','์ฒญํฌ๋'),
('orrange','tangerine','ํ
์ ๋ฆฐ'),
('orrange','navel orrange','๋ค์ด๋ธ ์ค๋ ์ง'),
('orrange','valencia orrange','๋ฐ๋ ์์ ์ค๋ ์ง');
๋ฐ์ดํฐ๋ ์ด๋ ๊ฒ insert ํ๋ค.
๊ณผ์ผ ์ข ๋ฅ๋ณ๋ก countํ๋ ์ฟผ๋ฆฌ๋ฌธ์ ์งฐ๋ค.
๋ฌธ์ (์ฝ๋ ๊ฐ์ )
ํญ๋ชฉ์ ์๋ ๊ฑด ์ ์ธํ๊ณ count ํ๊ธฐ
์๋ ์ฝ๋
select B.codekey, count(A.codekey)
from (select * from fruit where codetype like 'fruit') B
join fruit A
on A.codetype = B.codekey
group by B.codekey;
โ ๊ฐ์ ๋ ์ฝ๋
select min(B.codekey),
count(*) as "ํญ๋ชฉ์"
from fruit B,
fruit A
where 1=1
and A.codetype = B.codekey
group by B.codekey;
ํญ๋ชฉ์ ์๋ ๊ฒ๋ count ํ๊ธฐ
์๋ ์ฝ๋
select B.codekey, count(A.codekey)
from (select * from fruit where codetype like 'fruit') B
left join fruit A
on A.codetype = B.codekey
group by B.codekey;
โ ๊ฐ์ ๋ ์ฝ๋
select min(A.codekey),
count(B.*) as "ํญ๋ชฉ์"
from fruit A
left join fruit B
on A.codekey = B.codetype
where 1=1
and A.codetype = 'fruit'
group by A.codekey;
๊ฐ์ ๋ฐฉ์
1๏ธโฃ ๋ค์ฌ์ฐ๊ธฐ ์ ํด์ฃผ๊ธฐ
์๋ ์ฝ๋์ฒ๋ผ ๋ค์ฌ์ฐ๊ธฐ๋ฅผ ํ์ง ์๋๋ค๋ฉด ๊ฐ๋ ์ฑ์ด ๋จ์ด์ง๋ค.
(์ฝ๋์ ๊ธธ์ด๊ฐ ๊ธธ์ด์ง๋ฉด ๊ฐ๋ ์ฑ์ด ์์ฒญ ๋จ์ด์ง)
์ฌ๋๋ง๋ค ์คํ์ผ์ด ๋ค ๋ค๋ฅด๋ฏ๋ก ์ฐธ๊ณ ํด์ ์ฐ์ตํ๋๊ฒ ์ข๋ค.
2๏ธโฃ ๊ฐ๋ก๋ก ๊ธธ์ด์ง์ง ์๊ฒ ์ ์ ํ Enter ํด์ฃผ๊ธฐ
์ฝ๋ ์ค์ด ๊ธด ๊ฑด ์๊ด ์์ง๋ง, ๊ฐ๋ก๋ก ๊ธธ์ด์ง๋ฉด ๋ณด๊ธฐ ํ๋ค์ด์ง๊ธฐ ๋๋ฌธ์ ๊ฐ๋ก๋ก ๊ธธ์ด์ง๊ธฐ ์ ์ ์ค๋ฐ๊ฟ์ ํด์ฃผ๋ ๊ฒ ์ข๋ค.
SELECT A.column1,
A.column2,
B.column3,
MAX(B.column4) AS max_value
FROM
table1 A
LEFT JOIN table2 B
ON A.column1 = B.column1
GROUP BY
A.column1,
A.column2,
B.column3;
์ด๋ฐ ์์ผ๋ก Enter์ Tab์ ์ ์ ํ ์ฌ์ฉํด์ผ ํ๋ค.
3๏ธโฃ ์๋ธ์ฟผ๋ฆฌ ์ฌ์ฉ ์์
์๋ ์ฝ๋์๋ ๋ค์๊ณผ ๊ฐ์ ์๋ธ ์ฟผ๋ฆฌ๊ฐ ์๋ค.
from (select * from fruit where codetype like 'fruit') B
ํ์ง๋ง ์๋ธ ์ฟผ๋ฆฌ๋ ์ต!๋!ํ! ์ฌ์ฉ ์์ ํ๋ ๊ฒ ์ข๋ค.
๋ฐ์ดํฐ ์์ด ๋ฐฉ๋ํด์ง์๋ก ์๋๊ฐ ๋๋ ค์ง๋ค.
→ from ์ ์๋ ๋ ๊ฐ์ ํ ์ด๋ธ์ ํจ๊ป ์ฌ์ฉํ ์ ์๋ค.
4๏ธโฃ group by๋ ํตํฉํ ๋์๋ง ์ ์ด์ฃผ๋ ๊ฒ ์ข๋ค.
๊ทธ๋ผ ๋๋จธ์ง๋ ์ด๋ป๊ฒ ์ ์ด?
→ min()์ด๋ max()๋ก ํ์ํ๋ฉด ๋๋ค.
5๏ธโฃ Where ์ ์ 1=1 ์ ์ด์ฃผ๊ธฐ
SELECT *
FROM employees
WHERE 1=1
-- AND name = 'John'
-- AND age = 23
-- ์ฃผ์ ๋ฃ๊ธฐ๋ ํธํ๊ณ , ๋ชจ๋ ์กฐ๊ฑด์ ์ฃผ์์ ๋ฌ์๋ ํญ์ ์ฐธ์ด๋ค.
order by num;
์ฟผ๋ฆฌ๋ฌธ์ ์์ฑํ ๋, 1=1์ ๋ฃ์ด์ฃผ๋ฉด ๋ชจ๋ ์กฐ๊ฑด์ ์ฃผ์์ฒ๋ฆฌํด๋ ์ค๋ฅ๊ฐ ๋์ง ์๋๋ค.
์ด๋ WHERE ์ ์ 1=1์ด๋ผ๋ ์กฐ๊ฑด์ด ์์ผ๋ฉด, ์ด ์กฐ๊ฑด์ด ํญ์ ์ฐธ(TRUE)์ด๊ธฐ ๋๋ฌธ์ด๋ค.
์ฒ์ WHERE์ 1=1๋ฅผ ์ ์ด์ฃผ๊ณ ๋ค์ ์กฐ๊ฑด๋ถํฐ 'AND ์กฐ๊ฑด์'์ผ๋ก ์์ฑํ๋ฉด ํต์ผ์ฑ๋ ์๊ณ ์ฃผ์๋ ๋ฃ๊ธฐ ํธํ๊ธฐ ๋๋ฌธ์ ์ด๋ฐ ์์ผ๋ก ์์ฑํ๋ ๊ฒ์ ๊ถ์ฅํ๋ค.