TIL/SQL

프로그래머스| 우유와 요거트가 담긴 장바구니(My SQL)

jojoon2786 2024. 7. 31. 20:18

문제설명

조건 두개를 동시에 만족해야 하기에 self join을 활용하여 문제를 해결하였다.

 

나의 풀이)

select a.cart_id
from cart_products a join
(select cart_id, name
from cart_products) b on a.cart_id = b.cart_id
where a.name = "Milk" and b.name = "Yogurt"
group by cart_id
order by cart_id

하나의 쿼리에선 name이 Milk인, 하나의 쿼리에선 Yogurt인 cart_id에 대해 교집합을 구하였다.

^ㅁ^