表结构 ID UserIds
记录 1 2,3,4,
2 5,
3 2,
求一sql语句 找出userids字段含有2的记录 谢谢
这样最好啦,
select * from table1 where charindex (2,userids )!=0
to huohw:
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,
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