Mysql is the equivalent of an IN () clause that executes a logical AND instead

Let's say I have a table joining two tables ... like this:

id_product | id_category
-----------------
11 | 22
11 | 33
12 | 22
12 | 33

I want to get id_products (distinctly) according to the list of search category identifiers.

If I use the IN () clause, the id_categories list uses a logical OR.

How can I make a SELECT query have a logical AND for the list of sent id_categ ??

Example. I want all id_products belonging to categories 22 AND 33 (and possibly 5 more or more category identifiers)

I found this answer: Using the MySQL IN clause as all-inclusive (AND instead of OR) ... but the query mixes more than 1 table ... I only need a query for one table, one of them.

+5
source share
2

, , -

select id_product 
from yourTable
where id_category in (--your List goes Here)
group by id_product 
having count(distinct id_category) = NumberOfElementsOfYourList

=, id_category, id_category. , > =

+6
select id_product
from your_table
where id_category in (22, 33)
group by id_product
having count(distinct id_category) = 2

having, id_category. , , 5 , 2 5 having.

+1

All Articles