【问题标题】:How to select all strings the containing " n'th\product_table " from a column, in SQL using LIKE?如何在 SQL 中使用 LIKE 从列中选择包含“n'th\product_table”的所有字符串?
【发布时间】:2016-01-07 11:53:10
【问题描述】:

如何在 SQL 中使用 LIKE 从列中选择包含子字符串“n'th\product_table”的所有字符串?

示例:- 1)有一个table1,里面有如下数据

column1  
_ _    
"from n'th\product_table of period"  
"from n'th\product$table of period"  
"for of n'th\groduct$table of people"     
"to n'th\product_table of classes"  
"change n'th\producttable of record" 
"correct n'thproduct_table of value"

谁能给出一个使用 LIKE 运算符的 SQL 查询给出包含子字符串“n'th\product_table”的所有字符串的结果

预期结果

column1  
_ _    
"from n'th\product_table of period"
"to n'th\product_table of classes"

【问题讨论】:

  • 样本数据和期望的输出可以很好地提供准确的答案,不要浪费时间去猜测。
  • where col like '%n''th\product_table%'
  • @TonyAndrews 但它也会从 n'th\product$table of period 中选择该行,这不会发生
  • '%n''th\product\_table%'
  • @mnv 但它也会选择 for of n'th\groduct$table of people 的行,这不会发生跨度>

标签: sql oracle11g sql-like


【解决方案1】:

例子:

SELECT * FROM (
    SELECT 'from n\'th\\product_table of period' AS c
    UNION 
    SELECT 'from n\'th\\product$table of period'
) t
WHERE c LIKE '%n\'th\\\\product\_table%'

结果是

"from n'th\product_table of period"

【讨论】:

    【解决方案2】:

    您需要使用转义字符来使下划线表示下划线:

    where col like '%n''th\\product\_table%' escape '\'
    

    请注意,转义字符需要在字符串中实际的 '\' 以及 '_' 之前使用:

    where col like '%n''th\\product\_table%' escape '\'
                          ^        ^
    

    【讨论】:

      【解决方案3】:

      此方法使用 REGEXP_LIKE()。您只需将特殊字符 '\ 转义即可使它们成为文字:

      SQL> with tbl(str) as (
           select 'from n''th\product_table of period' from dual union
           select 'from n''th\product$table of period'   from dual union
           select 'for of n''th\groduct$table of people'  from dual union
           select 'to n''th\product_table of classes'   from dual union
           select 'change n''th\producttable of record'  from dual union
           select 'correct n''thproduct_table of value' from dual
         )
         select str
         from tbl
         where regexp_like(str, '.*n''th\\product_table.*');
      
      STR
      -----------------------------------
      from n'th\product_table of period
      to n'th\product_table of classes
      
      SQL>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-05-25
        • 2013-06-27
        • 1970-01-01
        • 2015-02-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多