日期:2014-05-18  浏览次数:20398 次

高手看过来。。。。急。。。。
A表中有一个字段Child_id
存放下列数据
2,3,4,5
6,8

B表中有一个字段Id
存放下列数据
1
2
3
4
5
6
7
请问如何将B表中的不在A表中的7找出来。
一条sql语句




------解决方案--------------------
create table A(Child_id nvarchar(20))
insert A select '2,3,4,5 '
union all select '6,8 '

create table B(Id int)
insert B select 1
union all select 2
union all select 3
union all select 4
union all select 5
union all select 6
union all select 7

select * from B
where not exists(select 1 from A where charindex( ', '+rtrim(ID)+ ', ', ', '+Child_id+ ', ')> 0)

--result
Id
-----------
1
7

(2 row(s) affected)