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.   

    select * from t1 where b like '%/[1/]%' ESCAPE '/'使用 ESCAPE 关键字定义转义符。在模式中,当转义符置于通配符之前时,该通配符就解释为普通字符
      

  2.   

    Create Table TEST
    (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]
    */
      

  3.   

    SELECT * FROM 表 where charindex(',1,',','+字段+',')>0
      

  4.   

    Create Table TEST
    (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,
    */
      

  5.   

    Create Table TEST
    (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
    */
      

  6.   

    问题工人解决了 charindex是sql函数??
      

  7.   

    charindex是MS SQL的函數,這個用charindex或者like都可以解決。第一個用charindex來做。Create Table TEST
    (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]
    */
      

  8.   

    问题虽然解决了 不过那些语句的语法看的不大懂!!Select * From TEST Where ','+A+',' Like '%,1,%'表A的旁边为什么要加 ','?? charindex(',1,',','+A+',')>0   为什么那么多,??'%[[]1]%'  这种语法什么意思?