这个查询怎么写??

bingbing1981 2004-11-16 06:41:36
一个表,两个字段
ID GroupID
001 1
001 2
001 3
002 1
002 2
003 1
. ..

现在想查询出GroupID等于1并且等于2并且等于3的ID是多少
该怎么写

想上面的例子,查出来的结果就是001
...全文
90 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
qizhanfeng 2004-11-17
  • 打赏
  • 举报
回复
select id
from tab
where groupid=1 or groupid=2 or groupid=3
group by id
having count(distinct groupid)=3
colinliu 2004-11-16
  • 打赏
  • 举报
回复
select id
from tab
where groupid=1 or groupid=2 or groupid=3
group by id
having count(distinct groupid)=3
up0point 2004-11-16
  • 打赏
  • 举报
回复
select id from tab a
where exists(select 1 from tab where id = a.id and groupid = 1)
and exists(select 1 from tab where id = a.id and groupid = 2)
and exists(select 1 from tab where id = a.id and groupid = 3)
j9898 2004-11-16
  • 打赏
  • 举报
回复
select id
from tab
where groupid=1 or groupid=2 or groupid=3
group by id
having count(distinct groupid)=3

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧