sql 记录完全包含 查询语句

jjcl521 2014-04-25 11:58:17

declare @tb table(sid int, code varchar(50), name varchar(50))

insert into @tb (sid, code, name)
select 1, '001', '1' union
select 1, '002', '2' union

select 2, '005','2' union
select 2, '006','3' union
select 2, '007','2'


declare @main table(sid int,title varchar(50))
insert into @main (sid, title )
select 1, '第一条数据' union
select 2, '第二条数据'


declare @main_z table(sid int,t_sid int, code_1 varchar(50), name_1 varchar(50))
insert into @main_z (sid, t_sid, code_1, name_1 )
select 1,1,'001','1' union
select 2,1,'002','2' union
select 3,1,'005','2' union

select 4,2,'001','1' union
select 5,2,'002','2' union
select 6,2,'005','2' union
select 7,2,'006','3' union
select 8,2,'007','2' union

select 9,3,'001','1' union
select 10,3,'005','2' union
select 11,3,'006','3'

select * from @tb
select * from @main
select * from @main_z
/*
--构造数据 若有问题请见谅
--@main a 主表 @main_z b子表 关联字段 a.sid=b.t_sid
--取@tb code name 完全包含在@main_z 表中的@main记录
--@tb表 sid=1 应该取到的
sid title
1 第一条数据
2 第二条数据

--@tb表 sid=2 应该取到的
sid title
2 第一条数据


*/
...全文
263 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
發糞塗牆 2014-04-25
  • 打赏
  • 举报
回复
tid=2的时候也是完全包含的吧
jjcl521 2014-04-25
  • 打赏
  • 举报
回复
引用 10 楼 DBA_Huangzj 的回复:
红字部分,不要动不动就用游标、用循环 SELECT * FROM @main WHERE sid IN ( select t_sid from @main_z b where exists( select * FROM @tb a where a.code= b.code_1 and a.name= b.name_1 AND a.sid in (1,2,3....) ) group BY t_sid HAVING COUNT(1)>=(SELECT COUNT(1) FROM @tb WHERE sid in (1,2,3....)))
我先测试一下 ,真正应用要关联好多表呢
發糞塗牆 2014-04-25
  • 打赏
  • 举报
回复
红字部分,不要动不动就用游标、用循环 SELECT * FROM @main WHERE sid IN ( select t_sid from @main_z b where exists( select * FROM @tb a where a.code= b.code_1 and a.name= b.name_1 AND a.sid in (1,2,3....) ) group BY t_sid HAVING COUNT(1)>=(SELECT COUNT(1) FROM @tb WHERE sid in (1,2,3....)))
jjcl521 2014-04-25
  • 打赏
  • 举报
回复
引用 7 楼 DBA_Huangzj 的回复:
sid=2的应该是第二条吧?你改变@id这个变量就可以了
sid=1 sid=2 是我假设的数据 如果 sid 会有很多呢 需要用游标遍历?
jjcl521 2014-04-25
  • 打赏
  • 举报
回复
引用 7 楼 DBA_Huangzj 的回复:
sid=2的应该是第二条吧?你改变@id这个变量就可以了
暂时测试 sql语句可以,我换到应用上去试试
發糞塗牆 2014-04-25
  • 打赏
  • 举报
回复
sid=2的应该是第二条吧?你改变@id这个变量就可以了
發糞塗牆 2014-04-25
  • 打赏
  • 举报
回复

declare @tb table(sid int, code varchar(50), name varchar(50))

insert into @tb (sid, code, name)
select 1, '001', '1' union
select 1, '002', '2' union

select 2, '005','2' union
select 2, '006','3' union
select 2, '007','2' 


declare @main table(sid int,title nvarchar(50))
insert into @main (sid, title )
select 1,N'第一条数据' union
select 2, N'第二条数据' 


declare @main_z table(sid int,t_sid int, code_1 varchar(50), name_1 varchar(50))
insert into @main_z (sid, t_sid, code_1, name_1 )
select 1,1,'001','1' union
select 2,1,'002','2' union
select 3,1,'005','2' union

select 4,2,'001','1' union
select 5,2,'002','2' union
select 6,2,'005','2' union
select 7,2,'006','3' union
select 8,2,'007','2' union

select 9,3,'001','1' union
select 10,3,'005','2' union
select 11,3,'006','3' 


DECLARE @id INT 
SET @id=2

SELECT *
FROM @main 
WHERE sid IN (
select t_sid
from @main_z b  
where exists( select * FROM @tb  a where   a.code= b.code_1 and a.name= b.name_1 AND  a.sid=@id )
group BY t_sid
HAVING COUNT(1)>=(SELECT COUNT(1) FROM @tb WHERE sid=@id))



/*
sid         title
----------- --------------------------------------------------
2           第二条数据

*/
jjcl521 2014-04-25
  • 打赏
  • 举报
回复
引用 3 楼 DBA_Huangzj 的回复:
我是说2的时候,对比了你的数据,应该有3行是满足的啊

--@tb sid=2

select * from @main_z b 

where exists( select * from @tb a where   a.code= b.code_1 and a.name= b.name_1 and a.sid=2)

--数据如下
sid t_sid code_1 name_1
3	1	005	2
6	2	005	2
7	2	006	3
8	2	007	2
10	3	005	2
11	3	006	3

--全部包含的只有 t_sid=2  ,t_sid=1,3只包含部分记录
發糞塗牆 2014-04-25
  • 打赏
  • 举报
回复
@tb.sid=2时,第一条也是满足的哦: declare @tb table(sid int, code varchar(50), name varchar(50)) insert into @tb (sid, code, name) select 1, '001', '1' union select 1, '002', '2' union select 2, '005','2' union select 2, '006','3' union select 2, '007','2' declare @main table(sid int,title nvarchar(50)) insert into @main (sid, title ) select 1,N'第一条数据' union select 2, N'第二条数据' declare @main_z table(sid int,t_sid int, code_1 varchar(50), name_1 varchar(50)) insert into @main_z (sid, t_sid, code_1, name_1 ) select 1,1,'001','1' union select 2,1,'002','2' union select 3,1,'005','2' union select 4,2,'001','1' union select 5,2,'002','2' union select 6,2,'005','2' union select 7,2,'006','3' union select 8,2,'007','2' union select 9,3,'001','1' union select 10,3,'005','2' union select 11,3,'006','3' --select * from @tb a SELECT *--DISTINCT a.SID,a.title from @main a INNER JOIN @main_z b ON a.sid=b.t_sid INNER JOIN @tb c ON c.name=b.name_1 AND c.code=b.code_1 WHERE c.sid=2 --WHERE EXISTS (SELECT 1 FROM (SELECT *FROM @tb WHERE sid=1) c WHERE b.code_1=c.code AND b.name_1=c.name ) /* --构造数据 若有问题请见谅 --@main a 主表 @main_z b子表 关联字段 a.sid=b.t_sid --取@tb code name 完全包含在@main_z 表中的@main记录 --@tb表 sid=1 应该取到的 sid title 1 第一条数据 2 第二条数据 --@tb表 sid=2 应该取到的 sid title 2 第一条数据 */ sid title sid t_sid code_1 name_1 sid code name ----------- -------------------------------------------------- ----------- ----------- -------------------------------------------------- -------------------------------------------------- ----------- -------------------------------------------------- -------------------------------------------------- 1 第一条数据 3 1 005 2 2 005 2 2 第二条数据 6 2 005 2 2 005 2 2 第二条数据 7 2 006 3 2 006 3 2 第二条数据 8 2 007 2 2 007 2
發糞塗牆 2014-04-25
  • 打赏
  • 举报
回复
我是说2的时候,对比了你的数据,应该有3行是满足的啊
jjcl521 2014-04-25
  • 打赏
  • 举报
回复
引用 1 楼 DBA_Huangzj 的回复:
tid=2的时候也是完全包含的吧
是的

27,579

社区成员

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

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