sql如何做这样的匹配查询?有一个字段:A 他的记录如下:
[1][12][8]
[8][1]
[20][11]
[11][12]
[18][1][11]我想选出这些记录中包含 [1] 的应该如何写sql语句?我这样写:select * from table where A like '%[1]%'包含[11]的记录也一起匹配进去了!! 我只想匹配含有 [1] 的记录!
[1][12][8]
[8][1]
[20][11]
[11][12]
[18][1][11]我想选出这些记录中包含 [1] 的应该如何写sql语句?我这样写:select * from table where A like '%[1]%'包含[11]的记录也一起匹配进去了!! 我只想匹配含有 [1] 的记录!
(A Varchar(100))
Insert TEST Select '[1][12][8]'
Union All Select '[8][1]'
Union All Select '[20][11]'
Union All Select '[11][12]'
Union All Select '[18][1][11]'
GO
Select * From TEST Where A Like '%[[]1]%'
GO
Drop Table TEST
--Result
/*
A
[1][12][8]
[8][1]
[18][1][11]
*/
(A Varchar(100))
Insert TEST Select '1,12,8,'
Union All Select '8,1,'
Union All Select '18,11,'
GO
Select * From TEST Where charindex(',1,',','+A+',')>0
drop table Test/*
1,12,8,
8,1,
*/
(A Varchar(100))
Insert TEST Select '1,12,8'
Union All Select '8,1'
Union All Select '20,11'
Union All Select '11,12'
Union All Select '18,1,11'
GO
Select * From TEST Where ','+A+',' Like '%,1,%'
GO
Drop Table TEST
--Result
/*
A
1,12,8
8,1
18,1,11
*/
(A Varchar(100))
Insert TEST Select '[1][12][8]'
Union All Select '[8][1]'
Union All Select '[20][11]'
Union All Select '[11][12]'
Union All Select '[18][1][11]'
GO
Select * From TEST Where CharIndex('[1]',A)>0
GO
Drop Table TEST
--Result
/*
A
[1][12][8]
[8][1]
[18][1][11]
*/