Data Science/SQL
[Elice SQL] full outer join을 union으로 할 때
Tashapark
2025. 3. 12. 21:49
728x90
* 마리아 디비는 full outer join 안 됨
수정 전
- 이게 도저히... 문제가 뭔지 모르겠었는데.
SELECT s.product_id, s.product_name, s.market_id, s.price AS market_price,
d.store_id, d.price AS store_price
FROM SUPERMARKET s
LEFT OUTER JOIN DEPARTMENT_STORE d ON s.product_id = d.product_id
UNION
SELECT s.product_id, s.product_name, s.market_id, s.price AS market_price,
d.store_id, d.price AS store_price
FROM SUPERMARKET s
RIGHT OUTER JOIN DEPARTMENT_STORE d ON s.product_id = d.product_id
ORDER BY product_id;
수정 후
- 우선 처음에는 left join에도 order by를 했었음.
- 그러면 누락이 생길 수 있기에 맨 마지막에 써야 함.
- right outer join이면 기준이 d로 바뀌어야 했는데.. 그대로 s로 해서 꼬였던 것.
- SUPERMARKET과 DEPARTMENT_STORE에서 취급되는 모든 상품을 조회하세요.
SELECT
s.product_id,
s.product_name,
s.market_id,
s.price AS market_price,
d.store_id,
d.price AS store_price
FROM SUPERMARKET s
LEFT OUTER JOIN DEPARTMENT_STORE d
ON s.product_id = d.product_id
UNION
SELECT
d.product_id, -- d가 기준이어야 함
d.product_name,
s.market_id,
s.price AS market_price,
d.store_id,
d.price AS store_price
FROM SUPERMARKET s
RIGHT OUTER JOIN DEPARTMENT_STORE d
ON s.product_id = d.product_id
ORDER BY product_id;
728x90
반응형