本文介绍了如何使用 IN 运算符但带有 AND 条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个表 Person
和 Interest
Person
表结构
Id | Name
兴趣
表结构
Id | Person Id | Interest Category
我的问题是我如何找到喜欢音乐"和电影"和运动"类别的人.类别将是动态的.
My question is how do I get person who have a liking for the category 'music' AND 'movie' AND 'sport'. The category will be dynamically.
所以它会喜欢使用 IN 运算符,但在 IN 运算符中使用 AND 条件而不是 OR 条件.
So it will like using IN operator but by using AND condition not OR condition in the IN operator.
SELECT `Person Id` FROM Interest
WHERE `Interest Category` IN ('music', 'movie', 'sport');
推荐答案
您可以使用 HAVING COUNT
.该数字也必须是动态的.
You could use HAVING COUNT
. The number would have to be dynamically too.
SELECT person_id
FROM interest
WHERE interest_category IN ( 'music', 'movie', 'sport');
GROUP BY person_id
HAVING COUNT(*) = 3;
这篇关于如何使用 IN 运算符但带有 AND 条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!