MySQL Query to find customers who have ordered two specific products

前端 未结 6 653
终归单人心
终归单人心 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 08:07

    This is an Access answer based on the infamous Northwind sample db. You should be abe to translate that in mySql quite easily.

    SELECT o.CustomerID, Sum([ProductID]='Prod1') AS Expr1, Sum([productid]='Prod1') AS Expr2
    FROM Orders AS o INNER JOIN [Order Details] AS d ON o.OrderID = d.OrderID
    GROUP BY o.CustomerID
    HAVING (((Sum([ProductID]='Prod1'))<>0) AND ((Sum([productid]='Prod1'))<>0));
    

提交回复
热议问题