我尝试选择team id=值列表的项;
例如:值列表为(9,7,6,4,3,1)
我想用team_id=(9,7,6,4,3,1)的项创建查询select;
我的尝试:

select item_id
from t_item as i join
     team as t
     on t.item_id = i.id
where t.id in (1221, 1219, 1);

但它工作起来就像in (1221 or 1219 or 1)但我想in(1221 and 1219 and 1)

最佳答案

我想你也需要group byhaving

select i.item_id
from t_item as i join
     team as t
     on t.item_id = i.id
where t.id in (1221, 1219, 1)
group by i.item_id
having count(distinct t.id) = 3;

注意,您不需要join
select t.item_id
from team t
where t.id in (1221, 1219, 1)
group by t.item_id
having count(distinct t.id) = 3;

如果team中的行是唯一的,则使用count(*)而不是count(distinct)

关于sql - SQL选择team_id为的所有项目(列表中的1和2以及3和4以及.. rest值),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51652229/

10-16 23:00