表名 table1
字段 id uid
1 1,2
2 2,6,11
3 2
4 3,4
5 4,5,7
6 6,9
如id=2,查詢得到
id uid
1 1,2
2 2,6,11
3 2
charindex(',',name)-1指定某字段中逗號開始的位置,-1是去除『,』本身的位置
sql code
create table table1 (id int, uid varchar(10))
insert into table1 values(1 , '1,2')
insert into table1 values(2 , '2,6,11')
insert into table1 values(3 , '2')
insert into table1 values(4 , '3,4')
insert into table1 values(5 , '4,5,7')
insert into table1 values(6 , '6,9')
godeclare @uid as int
set @uid = 2
select * from table1 where charindex(','+cast(@uid as varchar) + ',' , ','+ uid +',') > 0
/*id uid
----------- ----------
1 1,2
2 2,6,11
3 2
(所影響的行數為 3 行)
*/select * from table1 where ','+ uid +',' like '%,'+cast(@uid as varchar) + ',%'
/*id uid
----------- ----------
1 1,2
2 2,6,11
3 2
(所影響的行數為 3 行)*/
drop table table1
mysql 查詢以逗號相隔的字串
首先我們建立一張帶有逗號分隔的字串。create table test id int 6 not null auto increment,primary key id pname varchar 20 not null,pnum varchar 50 not null 然後插入帶有逗號分隔的測試資料...
生成查詢的模糊匹配字串 sql
if exists select from dbo.sysobjects where id object id n dbo f sql and xtype in n fn n if n tf drop function dbo f sql goif exists select from dbo.sy...
將有逗號的字串拆解
function regexp substr string,pattern,position,occurrence,modifier srcstr 需要進行正則處理的字串 pattern 進行匹配的正規表示式 position 起始位置,從第幾個字元開始正規表示式匹配 預設為1 occurrence...