MySQL Query to find customers who have ordered two specific products

前端 未结 6 597
终归单人心
终归单人心 2021-01-14 07:21

I\'m having trouble coming up with a query that will find all customers who have purchased both PROD1 and PROD2.

Here\'s a pseudo-query that kind of looks like what

6条回答
  •  余生分开走
    2021-01-14 07:56

    Example for sakila db:

    SELECT R.customer_id, GROUP_CONCAT(I.film_id) 
    FROM sakila.rental R 
    RIGHT OUTER JOIN sakila.inventory I ON R.inventory_id = I.inventory_id 
    WHERE I.film_id IN (22,44) GROUP BY R.customer_id HAVING COUNT(*) = 2
    

提交回复
热议问题