【问题标题】:Use of wild card character in escape()在 escape() 中使用通配符
【发布时间】:2019-08-07 17:01:43
【问题描述】:

如果我在 emp 表中输入 r__oshan 作为该列的值,则在 emp 表中有一个名为 'ename' 的列,然后我将 escape() 执行为“ select* from table name where columnname like ' %?______' escape' ? ;"然后我得到'r__oshan'作为输出。谁能帮我理解我是如何以及为什么得到这种类型的输出的?

【问题讨论】:

    标签: sql oracle oracle10g


    【解决方案1】:

    这个怎么样?见第 8 行。

    SQL> with emp (ename) as
      2    (select 'Little_Foot' from dual union all
      3     select 'r__oshan'    from dual union all
      4     select 'r_oshan'     from dual union all
      5     select 'roshan'      from dual
      6    )
      7  select * from emp
      8  where regexp_like(ename, '(\_)+');
    
    ENAME
    -----------
    Little_Foot
    r__oshan
    r_oshan
    
    SQL>
    

    再想一想,它可以更简单:

    where regexp_like(ename, '_+');
    

    【讨论】:

      【解决方案2】:

      你在问为什么like '%?______' escape '?' 匹配r__oshan

      只有第一个 _ 字符被转义,因此您的模式匹配任何后跟文字下划线 _,后跟任意五个字符的字符。 r__oshan 匹配该模式。

      【讨论】:

        【解决方案3】:

        您似乎在寻找解释,而不是“我该怎么做?”如果是这种情况,escape 关键字使用以下字符作为转义字符。在您的示例中,这意味着 ? “转义”以下字符,使其不被视为特殊字符。在like 语句中,_ 字符表示任何单个字符。

        例如,where 'x' like '_' 为真,where 'xx' like '_' 为假。当您转义特殊字符(在这种情况下为_)时,它只是按字面意思对待。所以现在,您实际上是在寻找 _ 字符。

        这是一个转义的例子:

        where 'x' like '?_' escape '?' /** Evaluates false **/
        where '_' like '?_' escape '?' /** Evaluates True **/
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-03-22
          • 2011-08-01
          • 2016-05-10
          • 2011-05-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多