Where Inner Join
INNER JOIN / WHERE ์ฐจ์ด
๋๋ฃ์ ์ฝ๋๋ฅผ ๋ณด๋ค INNER JOIN๊ณผ WHERE์ ํผ์ฉํด์ ์ฌ์ฉํ๊ธธ๋ ์ด๋ค๊ฒ์ด ๋ ์ข์ ์ฝ๋์ธ์ง ๊ถ๊ธํด์ ์ฐพ์๋ณด๊ธฐ๋ก ํ๊ณ ์ด๋ถ๋ถ์ ์ ์ฉํ์๋ค.
๋จผ์ JOIN๊ณผ WHERE์ ํผํฌ๋จผ์ค ์ธก๋ฉด์์๋ ๋์ผํ๋ค. ๊ณ์ ์ฌ์ฉํด๋ ๋ญ๊ฐ ํ๋ ํน๋ณํ ์ข์ ์ง๋ ๋ชฐ๋์์ง๋ง, ์ฝ๋๊ฐ ๊ธธ์ด์ง๋ฉด ๊ธธ์ด์ง์๋ก ์ฟผ๋ฆฌ๊ฐ ๋ณต์กํ๋ฉด ๋ณต์กํ ์๋ก WHERE๊ณผ ํ
์ด๋ธ์ JOINํ๋ ์ฝ๋๊ฐ ๋ฉ์ด์ ธ์ ๊ฒฐ๊ตญ ํผ์ฉํด์ ์ฌ์ฉํ๋ฉด ์ด๋ค ์กฐ๊ฑด์ด ๋ค์ด๊ฐ ์๋์ง ํ๋์ ์ฝ๊ธฐ ํ๋ค๋ค๋ ๊ฒ์ ๋๊ผ๋ค.
๊ทธ๋์ ๋ค๋ฅธ ์ฌ๋์ ์๊ฒฌ์ ์ฐธ๊ณ ํ์ฌ ์ฟผ๋ฆฌ๋ฌธ์ด ์ฑ๊ธ ํ ์ด๋ธ ์ผ ๊ฒฝ์ฐ WHERE์ ์ฌ์ฉ, ๋ฉํฐ ํ ์ด๋ธ์ผ ๊ฒฝ์ฐ INNER JOIN์ ์ฌ์ฉํ๊ธฐ๋ก ์ปจ๋ฒค์ ์ ์ ํ๋ค.
์๋ ์ฝ๋๋ ๋๋ฃ๊ฐ ๊ฐ๋ฐํ ์ฝ๋๋ค
SELECT ai.brand,
ai.model,
ai.pattern,
ai.min_bid,
b.name_image,
im.image,
ga.idx,
ga.start_dt,
ga.finish_dt,
ai.size,
ai.special_gift,
b.white_name
FROM auction_info ai
INNER JOIN goods g ON ai.goods_idx = g.idx
INNER JOIN item i ON g.item_idx = i.idx
INNER JOIN model m ON i.model_idx = m.idx
INNER JOIN brand b ON m.brand_code = b.code
INNER JOIN auction_image im ON im.auction_info_idx = ai.idx
INNER JOIN goods_auction ga ON ai.idx = ga.auction_info_idx
WHERE im.type = 'MF'
AND g.status = 'A005'
AND ga.is_ready_active = 1
AND ga.start_dt IS NOT NULL
AND ga.start_dt >= now()
ORDER BY ga.start_dt ASC
LIMIT 5;
์๋ ์ฝ๋๋ WHERE ์กฐ๊ฑด์ ์ ๋ถ INNER JOIN์ผ๋ก ์ฎ๊ฒผ๋ค
SELECT CONCAT(ai.model, ' ', ai.pattern, ' ', ai.size) as name,
ai.min_bid as minBbd,
b.name_image as nameImage,
im.image,
ga.idx,
ga.start_dt as startDt,
ga.finish_dt as finishDt,
ai.size,
ai.special_gift as specialGift,
b.white_name as whiteName
FROM auction_info ai
INNER JOIN goods g
ON ai.goods_idx = g.idx AND g.status = 'A005'
INNER JOIN item i ON g.item_idx = i.idx
INNER JOIN model m ON i.model_idx = m.idx
INNER JOIN brand b ON m.brand_code = b.code
INNER JOIN auction_image im
ON im.auction_info_idx = ai.idx
and im.type = 'MF'
INNER JOIN goods_auction ga
ON ai.idx = ga.auction_info_idx
AND ga.is_ready_active = 1
AND ga.start_dt IS NOT NULL
AND ga.start_dt >= now()
ORDER BY ga.start_dt ASC
LIMIT 5
๋ฌผ๋ก ๋์ค ์ด๋ค ์กฐ๊ฑด์์ ์ฃผ์ด๋ ๋์ผํ ๊ฒฐ๊ณผ์ ๋์ผํ ํผํฌ๋จผ์ค๊ฐ ๋์ค๊ฒ ์ง๋ง, ์ง์๊ฐ๋ฅํ ์ฝ๋๋ฅผ ์ํด ๊ฐ๋ ์ฑ์ ๋จผ์ ์๊ฐํ๋ ๋ด ์คํ์ผ๋ก๋ ์ด ๋ฐฉ์์ด ๋ ์๋ง๋๋ฏ ํ๋ค.
What is different between โINNER JOINโ and WHERE in mariaDB
I was reading my coworkerโs query code suddenly I was curios what is more better code between INNER JOIN and WHERE so I looked up and I changed code convention about this part.
First, JOIN and WHERE are equal performance. so I didnโt know what is better code before I read complicated query. When I try to read complicated query source, I realized it when I read complicated code.
Therefore, referring to the opinions of others, I got dcided to use WHERE if the query statement was a single table and INNER JOIN if it was a multi-table.
This is the coworker's code
SELECT ai.brand,
ai.model,
ai.pattern,
ai.min_bid,
b.name_image,
im.image,
ga.idx,
ga.start_dt,
ga.finish_dt,
ai.size,
ai.special_gift,
b.white_name
FROM auction_info ai
INNER JOIN goods g ON ai.goods_idx = g.idx
INNER JOIN item i ON g.item_idx = i.idx
INNER JOIN model m ON i.model_idx = m.idx
INNER JOIN brand b ON m.brand_code = b.code
INNER JOIN auction_image im ON im.auction_info_idx = ai.idx
INNER JOIN goods_auction ga ON ai.idx = ga.auction_info_idx
WHERE im.type = 'MF'
AND g.status = 'A005'
AND ga.is_ready_active = 1
AND ga.start_dt IS NOT NULL
AND ga.start_dt >= now()
ORDER BY ga.start_dt ASC
LIMIT 5;
so I changed like this
SELECT CONCAT(ai.model, ' ', ai.pattern, ' ', ai.size) as name,
ai.min_bid as minBbd,
b.name_image as nameImage,
im.image,
ga.idx,
ga.start_dt as startDt,
ga.finish_dt as finishDt,
ai.size,
ai.special_gift as specialGift,
b.white_name as whiteName
FROM auction_info ai
INNER JOIN goods g
ON ai.goods_idx = g.idx AND g.status = 'A005'
INNER JOIN item i ON g.item_idx = i.idx
INNER JOIN model m ON i.model_idx = m.idx
INNER JOIN brand b ON m.brand_code = b.code
INNER JOIN auction_image im
ON im.auction_info_idx = ai.idx
and im.type = 'MF'
INNER JOIN goods_auction ga
ON ai.idx = ga.auction_info_idx
AND ga.is_ready_active = 1
AND ga.start_dt IS NOT NULL
AND ga.start_dt >= now()
ORDER BY ga.start_dt ASC
LIMIT 5
Of course, any conditional expression of the two will produce the same result and the same performance, but this method seems to fit better with my style of thinking about readability first for sustainable code.