我是SQL sever 的学习新手,现在有张学生数据信息表,有个学生的名字是“王_林”,想通过模式匹配的语句查询他的信息,讲到了取笑通配符的匹配功能,但是应用时出现了一些问题,请教大家:(1)select *from xs where xm like '%王__林'escape '_';
(2)select *from xs where xm like '%__林'escape '_';
(3)select *from xs where xm like '%王_林'escape '_';
(4)select *from xs where xm like '王_林'escape '_';
(5)select *from xs where xm like '%王_ _林'escape '_';
以上这五个语句查询的结果都不一样,要取消下划线‘_'的统配功能,语法格式用select *from xs where string_expression like string_expression escape 'escape_character',但是上面的五句中只有第五句能够查到“王_林”,这是为什么呢?将第五句中的 '%王_ _林'改为’%王_林‘后查询为空,这又是为什么?请大家帮下忙,谢谢!

解决方案 »

  1.   


    declare @xs table (xm varchar(5))
    insert into @xs
    select '王_林'--select * from @xsselect *from @xs where xm like '%王__林'escape '_';
    select *from @xs where xm like '%__林'escape '_';
    select *from @xs where xm like '%王_林'escape '_';
    select *from @xs where xm like '王_林'escape '_';
    select *from @xs where xm like '%王_ _林'escape '_';第一句和第二句都可以查出来呀
      

  2.   


    declare @xs table (xm varchar(5))
    insert into @xs
    select '王_林' union all
    select '王 林' union all
    select '王林'--select * from @xsselect *from @xs where xm like '%王__林'escape '_';--两个下划线,第一个理解为转义字符
    /*
    xm
    -----
    王_林
    */
    select *from @xs where xm like '%__林'escape '_';
    /*
    xm
    -----
    王_林
    */
    select *from @xs where xm like '%王_林'escape '_';
    /*
    xm
    -----
    王林
    */
    select *from @xs where xm like '王_林'escape '_';
    /*
    xm
    -----
    王林
    */
    select *from @xs where xm like '%王_ _林'escape '_';
    /*
    xm
    -----
    王 林
    */
      

  3.   

    请帮我分析一下模式匹配中escape的用法,谢谢!
    以上的五句是我在实验过程中修改出来的,但是不明白是什么意思,
    希望能够得到解答,谢谢!
      

  4.   

    以上的五句是我在实验过程中修改出来的,但是不明白是什么意思,
    希望能够得到解答,谢谢!
    前两句能够查出,大好似好像后面的escape语句失去了意义,
    请求懂的人解释一下escape语句的用法,及上面各句的意思,特别是各个通配符的用法和不同位置语句的意义,谢谢!
      

  5.   

    1.使用   ESCAPE   关键字定义转义符。在模式中,当转义符置于通配符之前时,该通配符就解释为普通字符。例如,要搜索在任意位置包含字符串   5%   的字符串,请使用:       WHERE   ColumnA   LIKE   '%5/%%'   ESCAPE   '/'   即‘/’为转义字符,第二个‘%’为普通字符,第一、第三个为通配符。
    2.ESCAPE   'escape_character'     
      允许在字符串中搜索通配符而不是将其作为通配符使用。escape_character   是放在通配符前表示此特殊用途的字符。   
      SELECT   *   
      FROM   finances   
      WHERE   description   LIKE   'gs_'   ESCAPE   'S'   
      GO    
      意思就是:   
      比如,我们要搜索一个字符串     "g_"     ,如果直接     like     "g_",那么   "_"的作用就是通配符,而不是字符,结果,我们会查到比如     "ga","gb","gc",而不是我们需要的   "g_".   
      用     LIKE   'gs_'   ESCAPE   'S'     's'表示特殊用法标志。就会搜索‘s_’了。 
    3.create   table   a   (name   varchar(10))   
      go   
      insert   into   a   select   '11%22'   
      union   all   select   '11%33'   
      union   all   select   '12%33'   
      go   
      select   *   from   a     WHERE   name   LIKE   '%/%33'   ESCAPE   '/'   --指定用'/'符号来说明跟在其后面的通配符字符为普能字符。(第二个%是字符不是通配符来的)   
      go   
      drop   table   a 
    结果为:   
      name                 
      ----------     
      11%33   
      12%33   
      

  6.   

    也可以使用[]将通配符放在里面。
    借用3楼例子:
    declare @xs table (xm varchar(5))
    insert into @xs
    select '王_林' union all
    select '王 林' union all
    select '王林'select * from @xs where xm like '%王[_]%'
    结果:
    xm
    王_林
      

  7.   

     明白了!
    1.使用   ESCAPE   关键字定义转义符。在模式中,当转义符置于通配符之前时,该通配符就解释为普通字符。例如,要搜索在任意位置包含字符串   5%   的字符串,请使用:       WHERE   ColumnA   LIKE   '%5/%%'   ESCAPE   '/'   即‘/’为转义字符,第二个‘%’为普通字符,第一、第三个为通配符。
    2.ESCAPE   'escape_character'     
      允许在字符串中搜索通配符而不是将其作为通配符使用。escape_character   是放在通配符前表示此特殊用途的字符。   
      SELECT   *   
      FROM   finances   
      WHERE   description   LIKE   'gs_'   ESCAPE   'S'   
      GO    
      意思就是:   
      比如,我们要搜索一个字符串     "g_"     ,如果直接     like     "g_",那么   "_"的作用就是通配符,而不是字符,结果,我们会查到比如     "ga","gb","gc",而不是我们需要的   "g_".   
      用     LIKE   'gs_'   ESCAPE   'S'     's'表示特殊用法标志。就会搜索‘s_’了。 
    3.create   table   a   (name   varchar(10))   
      go   
      insert   into   a   select   '11%22'   
      union   all   select   '11%33'   
      union   all   select   '12%33'   
      go   
      select   *   from   a     WHERE   name   LIKE   '%/%33'   ESCAPE   '/'   --指定用'/'符号来说明跟在其后面的通配符字符为普能字符。(第二个%是字符不是通配符来的)   
      go   
      drop   table   a 
    结果为:   
      name                 
      ----------     
      11%33   
      12%33   
      

  8.   


    我感觉‘__’ 这是2个‘_’,即使你删了一个,还有一个,这样的话,这个就代表个字。
    而且,这种查询还有根据要查询的实际数据,如果你这样'%王_ _林'escape '_' 上面根本就不存在这样的数据,所以为null