--测试环境 declare @t table(ID int identity(1,1),UserIds varchar(20)) insert into @t select '2,3,4' union all select '5,1' union all select '2,' union all select '1,2,' union all select '1, 12,'--执行查询 select * from @t where charindex(',2,',','+UserIds+',')>0--结果ID UserIds ----------- -------------------- 1 2,3,4 3 2, 4 1,2,(所影响的行数为 3 行)
select * from ta where userids like '%,2,%' or userids like '2,%'
如此: select * from table1 where userids like '%,2,%' or userids like '2,%' or userids like '%,2'
--注意:下面的写法是不对的, 楼上的也不行.
--select * from ta where userids like '%2%'
select * from table1 where charindex ('2',userids )!=0
老大们解释一下,这样的为什么不行啊?
ID UserIds
1 2,3,4,
2 5,1
3 2,
4 1, 2,
5 1, 12,
找出userids字段含有2的记录,我想结果应该是:
1 2,3,4,
3 2,
4 1, 2,
declare @t table(ID int identity(1,1),UserIds varchar(20))
insert into @t select '2,3,4'
union all select '5,1'
union all select '2,'
union all select '1,2,'
union all select '1, 12,'--执行查询
select * from @t where charindex(',2,',','+UserIds+',')>0--结果ID UserIds
----------- --------------------
1 2,3,4
3 2,
4 1,2,(所影响的行数为 3 行)
select * from table1 where userids like '%,2,%' or userids like '2,%' or userids like '%,2'
在str1中寻找str2是否存在