日期:2014-05-17 浏览次数:20900 次
create table zh
(ID varchar(15),NAME varchar(10))
insert into zh
select '1,2,3,4,5', 'Name1' union all
select '1,2,4,5', 'Name1' union all
select '2,3', 'Name2' union all
select '1,4', 'Name3' union all
select '3', 'Name4' union all
select '2', 'Name5' union all
select '1,2,3,4', 'Name6'
declare @x varchar(10)
select @x='1,2,3,4'
select ID,NAME from
(select a.ID,a.NAME,
case when charindex(substring(a.ID,b.number,charindex(',',a.ID+',',b.number)-b.number),@x,1)>0
then 1 else 0 end 'h'
from zh a, master.dbo.spt_values b
where b.type='P' and b.number between 1 and len(a.ID) and substring(','+a.ID,b.number,1)=',') t
group by ID,NAME
having count(1)=sum(h)
/*
ID NAME
--------------- ----------
2,3 Name2
1,4 Name3
3 Name4
2 Name5
1,2,3,4 Name6
(5 row(s) affected)
*/
;with cte(ID,name) as
(
select '1,2,3,4,5','Name1'
union all select '1,2,4,5','Name1'
union all select '2,3','Name2'
union all select '1,4','Name3'
union all select '3','Name4'
union all select '2','Name5'
union all select&nb