相当于IN()子句的mysql做逻辑AND [英] mysql equivalent of IN() clause doing logical AND's instead

查看:80
本文介绍了相当于IN()子句的mysql做逻辑AND的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个在两个表之间建立连接的表...像这样:

Let's say I have a table making a junction between two tables... like this:

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

我想根据搜索到的类别ID列表获得id_products(不同).

I want to get id_products (distinct) according to a list of searched categories IDs.

如果我使用IN()子句,则id_categories的列表使用逻辑或.

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

如何进行SELECT查询以对提交的id_categ列表进行逻辑AND?

How can I make a SELECT query to have logical ANDs for the list of id_categ submitted??

示例:我希望所有属于类别22 AND 33的id_products(可能还有5个以上的类别ID)

Example: I want all the id_products belonging to category 22 AND 33 (and possibly 5+ more Categ. IDs)

我找到了这个答案: 将MySQL IN子句全部包含在内(AND代替的) ...但是该查询混合了多个表...我只想查询单个表,即联结表.

I found this answer: Using MySQL IN clause as all inclusive (AND instead of OR) ...but the query is mixing more than 1 table... I only want a query on a single table, the junction one.

推荐答案

阅读您的链接,我想这就像

reading your link, I think it would be something like

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,则应使用=. 如果没有,请使用> =

you should use = if only wants to get that id_category, but no others id_category. If not, use >=

这篇关于相当于IN()子句的mysql做逻辑AND的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆