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
반응형
'Data Science > SQL' 카테고리의 다른 글
[Elice sql] 서브쿼리에서는 on 아니고 where로 조건 줄 것. exists 사용이 더 나음. (0) | 2025.03.13 |
---|---|
[Elice sql] cte..는 계속 헷갈림. (0) | 2025.03.13 |
[Elice sql] from 서브쿼리 별칭을 명시해야 오류 x (0) | 2025.03.11 |
[Elice sql] 그룹 함수 (0) | 2025.03.11 |
[Elice sql] 윈도우 함수 (0) | 2025.03.11 |