22,207
社区成员
发帖
与我相关
我的任务
分享
create table tb1(id int,name varchar(10))
insert into tb1 select 101 , 'AAAA'
insert into tb1 select 102 , 'BBBB'
insert into tb1 select 103 , 'CCCC'
insert into tb1 select 104 , 'DDDD'
create table tb2(id int,aid varchar(50))
insert into tb2 select 601,'101,102'
go
select top 50 id=identity(int,1,1) into # from syscolumns a
select a.id,b.name
from
(
select a.id,aid=substring(a.aid,b.id,charindex(',',a.aid+',',b.id)-b.id)
from tb2 a,# b
where substring(','+a.aid,b.id,1)=','
) a,tb1 b
where a.aid=b.id
drop table tb1,tb2,#
/*
id name
----------- ----------
601 AAAA
601 BBBB
(所影响的行数为 2 行)
*/
--> 测试数据: #A
if object_id('tempdb.dbo.#A') is not null drop table #A
create table #A (ID int,名称 varchar(4))
insert into #A
select 101,'AAAA' union all
select 102,'BBBB' union all
select 103,'CCCC' union all
select 104,'DDDD'
--> 测试数据: #B
if object_id('tempdb.dbo.#B') is not null drop table #B
create table #B (ID int,AIDs varchar(100))
insert into #B
select 601,'101,102'
select * from #A t where not exists (select 1 from #B where charindex(','+ltrim(t.ID)+',', ','+AIDs+',')>0)
/*
ID 名称
----------- ----
103 CCCC
104 DDDD
*/