๐™ณ๐™ฐ๐šƒ๐™ฐ๐™ฑ๐™ฐ๐š‚๐™ด

[SQL] LEFT JOIN์„ ํ™œ์šฉํ•œ ๋ˆ„๋ฝ๋œ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌํ•˜๊ธฐ

ํ•ด๋ฒ„๋‹ˆ 2024. 4. 19. 14:34
๋ฐ˜์‘ํ˜•

์œ„ ํ‘œ๋ฅผ ๋ณด๋ฉด codetype์— fruit์ด ์žˆ๊ณ  codekey์—๋Š” ๊ณผ์ผ์˜ ์ข…๋ฅ˜๊ฐ€ ์ ํ˜€์žˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๊ฐ๊ฐ ๊ณผ์ผ์— ๋Œ€ํ•œ ์ข…๋ฅ˜์™€ ์ด๋ฆ„์ด ๋‚˜์™€์žˆ๋‹ค. 

 

ํ˜„์žฌ ํฌ๋„๋Š” 4๊ฐœ, ์‚ฌ๊ณผ๋Š” 2๊ฐœ,  ์˜ค๋ Œ์ง€๋Š” 3๊ฐœ, ๋ฐฐ๋Š” 0๊ฐœ์ธ๋ฐ

ํฌ๋„, ์‚ฌ๊ณผ, ์˜ค๋ Œ์ง€, ๋ฐฐ ๋ณ„๋กœ ๊ฐœ์ˆ˜๋ฅผ ์„ธ๋Š” ์ฟผ๋ฆฌ๋ฌธ์„ ๋งŒ๋“ค๊ณ  ์‹ถ์—ˆ๋‹ค. 

 

 

 

 

 

ํ•ญ๋ชฉ์— ์—†๋Š” ๊ฑด ์ œ์™ธํ•˜๊ณ  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;

 

 

 

 

 

ํ•ญ๋ชฉ์— ์—†๋Š” ๊ฒƒ๋„ count ํ•˜๊ธฐ

๋ฐฐ๋Š” 0๊ฐœ์ธ๋ฐ 0๊ฐœ๋„ ์ถœ๋ ฅํ•˜๊ณ  ์‹ถ๋‹ค!

left join์„ ์‚ฌ์šฉํ•˜์—ฌ ๋งŒ๋“ค์—ˆ๋‹ค. 

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;

 

 

 

 

 

๋ฐ˜์‘ํ˜•