Reputation: 23
I have three tables:
Customer table
Product Table
Customer_Product Table
I need to make query: Search for customers who bought a certain product (for example, name = "toilet paper") at least 2 times I don't understand how.. I'm noob in databases.. Please help
Upvotes: 1
Views: 36
Reputation: 50173
You need aggregation :
select c.name
from customer c inner join
customer_product cp
on cp.customer_id = c.customer_id inner join
product p
on p.product_id = cp.product_id
where p.lable = 'toilet paper'
group by c.name
having count(p.lable) > 1;
Upvotes: 1