我有一张小桌子,其中包含要查找查询的组成员身份。
uid groupid userid
1 2 5
2 2 6
3 1 2
4 3 8
5 4 7
我想知道如果两个给定的用户ID在同一组中是否可以返回TRUE?
以下内容获取具有两个给定成员的所有组:
select groupid
from table t
where userid in ($userid1, $userid2)
group by groupid
having count(distinct userid) = 2;
如果愿意,可以将其转换为布尔值:
select (case when count(*) > 0 then true else false end)
from (select groupid
from table t
where userid in ($userid1, $userid2)
group by groupid
having count(distinct userid) = 2
) g;
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句