G
G
Gura2016-08-09 16:33:44
MySQL
Gura, 2016-08-09 16:33:44

How to select data from mysql table by multiple categories?

Hello, I'm already sick of digging Google and Yashki. It seemed to me that I knew mysql well and was able to create a complex query, but it turned out not. mysql experts help!
I have three tables (simplified version):
products(id), categories(id), product_categories(product_id, category_id)
I need to select data (product) from the products table if this product fits into the categories I need.
Let's say I have 3 items, I need to select only those items in which the category is round or oval. But the trick is that one item can be in two or more categories at once.
To put it simply, I need to select data by analogy with filters in online stores. Let's display all products that are red and square. Restrictions should also work, that is, display information strictly if this product is included in the selected categories.
Sorry if I explained something wrong. Thank you in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Gura, 2016-08-09
@Gudzera

SELECT i.* FROM products p JOIN filters f USING (id) WHERE f.filter IN (2,3,7) GROUP BY p.id HAVING COUNT(DISTINCT f.filter) = колличество фильтров

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question