日期:2014-05-18 浏览次数:20560 次
select * from tb where charindex(',2,',','+ids+',')>0 or charindex(',11,',','+ids+',')>0
------解决方案--------------------
select * from tb where charindex(',2,',','+ids+',')>0 or charindex(',11,',','+ids+',')>0
------解决方案--------------------
create table A(ids varchar(100)) insert into A select '2,21,23,27' union all select '1,7,11,12' union all select '5' union all select '6,8' go declare @M varchar(50), @N xml set @M='2,9,11' set @N=convert(xml,'<r><c>'+replace(@M,',','</c><c>')+'</c></r>') ;with cte as ( select col=T.c.value('.[1]','varchar(20)') from @N.nodes('/r/c') T(c) ) select a.col from cte a left join A b on charindex(','+a.col+',',','+b.ids+',') > 0 where b.ids is null drop table A /************ col -------------------- 9 (1 行受影响)
------解决方案--------------------
--> 测试数据:[A1] if object_id('[A1]') is not null drop table [A1] create table [A1]([ids] varchar(10)) insert [A1] select '2,21,23,27' union all select '1,7,11,12' union all select '5' union all select '6,8' create table #tt( ids varchar(2) ) declare @str varchar(100) set @str='1,9,11' select @str='insert #tt select '+REPLACE(@str,',',' union all select ') exec(@str) select * from #tt a where exists(select 1 from [A1] b where CHARINDEX(','+a.ids+',',','+b.ids+',')>0) /* ids 1 11*/
------解决方案--------------------
建议不要用一个字段存放多个数据的方法,这种会导致查询的时候效率特别低下,往往查询的时间通常在100毫秒以上,甚至达到秒级的查询速度。
如果改成单个表来描述数据关系,查询时间通常在10毫秒以内。