怎样实现这个SQL语句?

xwk789789xwk 2011-06-27 09:13:12
表A有列
id name code_name code
1 张三 aaa 001
1 张三 aaa 002
2 李四 aaa 001
3 王五 bbb 002

表B有列
name code
aaa 001
bbb 002

各位好,向大家请教个问题:有表A跟表B如上面所示,表B有001和002两个限制,则表A中每个人必须包含001和002两条数据,怎样用sql语句查询出表A中不满足条件的数据(即查询出表A中不同时包含001和002的人的姓名)? 上表查询出来应该是“李四”和“王五”,因为“张三”同时包含了001和002.
...全文
116 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
firecc05 2011-07-01
  • 打赏
  • 举报
回复
select A.name
from A
where A.code in (select code from B)
group by A.name
having count(distinct A.code) <(select count(1) from B)
-------------------------
select distinct A1.name
from A A1
where exists(select * from B where B.code not in(select code from A A2 where A2.name=A1.name))
weiyangzhizhu 2011-07-01
  • 打赏
  • 举报
回复
select A.name
from A
join B on A.code=B.code
group by A.name
having count(distinct A.code) < (select count(*) from B)
terry_zyl 2011-07-01
  • 打赏
  • 举报
回复
不错不错,,快给分啊。。。。
yubofighting 2011-07-01
  • 打赏
  • 举报
回复

select name from A where (select count(name) from A where a.code='001' and b.code='002')<2
jiangzhong610 2011-07-01
  • 打赏
  • 举报
回复

SELECT * FROM 表A where code_name+code not IN(SELECT name+code from 表B )
jiangzhong610 2011-07-01
  • 打赏
  • 举报
回复

SELECT * FROM 表A where code_name+code IN(SELECT name+code from 表B )
gzycw 2011-06-28
  • 打赏
  • 举报
回复
select * from a group by name,code_name having count(*)<(select count(*) from b)
cd731107 2011-06-28
  • 打赏
  • 举报
回复
select A.name
from A
group by A.name
having count(distinct A.code) < (select count(*) from B)
--小F-- 2011-06-27
  • 打赏
  • 举报
回复
select
A.name
from
A,B
where
A.code=B.Code
group by
A.name
having
count(distinct A.code)=(select count(1) from B)
AcHerat 2011-06-27
  • 打赏
  • 举报
回复

select A.name
from A,B
where A.code=B.Code
group by A.name
having count(distinct A.code) < (select count(*) from B)
liangCK 2011-06-27
  • 打赏
  • 举报
回复
select A.name
from A,B
where A.code=B.Code
group by A.name
having count(distinct A.code)=(select count(*) from B)

27,579

社区成员

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

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