sql 表b 做整体查询

qustzhou 2008-01-07 04:20:43
三表A
数据如下

id pid num
1 2 5
1 3 7
1 4 9
4 2 5
5 4 9
6 3 7
2 2 5
2 3 7
2 4 9
2 2 5

表B
数据如下

pid num
2 5
3 7
4 9


把表B的数据作为条件,pid,num看成一组数据,并且一个表作整体条件
只有表B所有的数据都包含在表A中 查出相应的id

表A中不能部分的与表B所对应

上面输出的结果应该为:

id
1
2






...全文
89 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
changjiangzhibin 2008-01-07
  • 打赏
  • 举报
回复
up
qustzhou 2008-01-07
  • 打赏
  • 举报
回复
发表于:2008-01-07 16:20:43 楼主
三表A
数据如下

id pid num
1 2 5
1 3 7
1 4 9
4 2 5
5 4 9
6 3 7
2 2 5
2 3 7
2 4 9
2 6 5

表B
数据如下

pid num
2 3
3 2
4 2


把表B的数据作为条件,pid,num看成一组数据,并且一个表作整体条件
只有表B所有的数据都包含在表A中 查出相应的id

表A中不能部分的与表B所对应

上面输出的结果应该为:

id
1
2


--如果一个pid可以对应多个num(谢谢dobear_0922 )这个是数据弄错了,不存在那种情况
不过如果表B中的pid所对应的num可以比表A中的小

呵呵,我当初差一点用了循环来,利用表B的条件一条一条的筛选表A中的数据



kk19840210 2008-01-07
  • 打赏
  • 举报
回复


select distinct(id) from #a where id not in
(
select distinct c.id from #a right join
(select a.*,b.* from #b b, (select distinct(id) from #a)a)c on c.pid=#a.pid and c.num=#a.num and #a.id=c.id
where #a.id is null
)


id
-----------
1
2

(2 行受影响)
dobear_0922 2008-01-07
  • 打赏
  • 举报
回复
create table A(id int, pid int, num int)
create table B(pid int, num int)
insert A select 1, 2, 5
union all select 1, 3, 7
union all select 1, 4, 9
union all select 4, 2, 5
union all select 5, 4, 9
union all select 6, 3, 7
union all select 2, 2, 5
union all select 2, 3, 7
union all select 2, 4, 9
union all select 2, 2, 5

insert B select 2, 5
union all select 3, 7
union all select 4, 9

--如果一个pid可以对应多个num
select distinct id
from A
where not exists(select 1 from B
left join (select pid, num from A A1 where id=A.id) T
on B.pid=T.pid and B.num=T.num
where T.pid is null)

/*
id
-----------
1
2

(2 row(s) affected)
*/

drop table A,B
-狙击手- 2008-01-07
  • 打赏
  • 举报
回复
set nocount on
declare @ta table(id int,pid int,num int)
insert @ta select 1 ,2, 5
insert @ta select 1 ,3, 7
insert @ta select 1 ,4 ,9
insert @ta select 4, 2 ,5
insert @ta select 5 ,4 ,9
insert @ta select 6 ,3 ,7
insert @ta select 2 ,2, 5
insert @ta select 2, 3 ,7
insert @ta select 2 ,4 ,9
insert @ta select 2, 2, 5

declare @tb table(pid int,num int)
insert @tb select 2,5
insert @tb select 3 ,7
insert @tb select 4, 9


select distinct id
from @ta a
where not exists(select 1
from @tb b
where not exists (select 1 from @ta T where id=A.id and pid = b.pid and num = b.num))

/*

id
-----------
1
2

*/
dobear_0922 2008-01-07
  • 打赏
  • 举报
回复
create table A(id int, pid int, num int)
create table B(pid int, num int)
insert A select 1, 2, 5
union all select 1, 3, 7
union all select 1, 4, 9
union all select 4, 2, 5
union all select 5, 4, 9
union all select 6, 3, 7
union all select 2, 2, 5
union all select 2, 3, 7
union all select 2, 4, 9
union all select 2, 2, 5

insert B select 2, 5
union all select 3, 7
union all select 4, 9

select distinct id
from A
where not exists(select pid from B where pid not in (select pid from A T where id=A.id))

/*
id
-----------
1
2
*/

drop table A,B
qustzhou 2008-01-07
  • 打赏
  • 举报
回复
发表于:2008-01-07 16:20:43 楼主
三表A
数据如下

id pid num
1 2 5
1 3 7
1 4 9
4 2 5
5 4 9
6 3 7
2 2 5
2 3 7
2 4 9
2 2 5

表B
数据如下

pid num
2 5
3 7
4 9


把表B的数据作为条件,pid,num看成一组数据,并且一个表作整体条件
只有表B所有的数据都包含在表A中 查出相应的id

表A中不能部分的与表B所对应

上面输出的结果应该为:

id
1
2
dobear_0922 2008-01-07
  • 打赏
  • 举报
回复
--try
select distinct id
from A
where not exists(select pid from B where pid not in (select pid from A T where id=A.id))

22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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