【问题标题】:WILDCARDS in SQL Oracle [duplicate]SQL Oracle 中的通配符 [重复]
【发布时间】:2020-01-31 12:26:56
【问题描述】:

我试图在 Oracle DB 中查找以 A、B 或 C 开头的所有名称,我编写了以下语法:

SELECT NUME FROM JUCATORI
WHERE NUME LIKE '[ABC]%';

但它没有给我任何名称(也没有错误),即使我确定我的数据库名称中的名称以 A、B 或 C 开头。

【问题讨论】:

    标签: oracle wildcard


    【解决方案1】:

    LIKE 不适用于字符集。 (我认为 T-SQL 扩展了 LIKE 以处理此类表达式,但 Oracle 的 SQL 没有。)您不会收到错误,因为 LIKE '[ABC]%' 会查找以左括号开头的字符串,然后是 A,然后是 B,然后是 C,然后是右括号

    所以要么使用OR:

    SELECT nume FROM jucatori WHERE nume LIKE 'A%' OR nume LIKE 'B%' OR nume LIKE 'C%';
    

    或正则表达式:

    SELECT nume FROM jucatori WHERE REGEXP_LIKE(nume, '^[ABC]');
    

    【讨论】:

    • 除了它可以使用通配符 - '%' 是通配符。它不适用于 '[ABC]' 的字符集。
    • @piezol:是的,这当然是正确的,毕竟这就是LIKE 的全部内容:-) 我会相应地更新我的答案。谢谢。
    【解决方案2】:

    一个选项是

    where substr(nume, 1, 1) in ('A', 'B', 'C')
    

    【讨论】:

      【解决方案3】:

      您将正则表达式与“喜欢”条件混淆了。

      Like 检查您传递给它的字符串,只有三个例外:

      1. %    - any number of any characters
      2. _    - any single character
      
      3. ESCAPE clause that lets you define escape character 
      so you can use %  as 'I'm looking for percent sign'
      

      [ABC] 意思是“A、B 或 C”是正则表达式语法。 您可能想改用 regexp_like,或者对于这样简单的查询,只需“或”几个喜欢:

      with examples as (
         select 'Adam' ex from dual union all
         select 'Dorothy' ex from dual union all
         select '[ABC]something' ex from dual union all
         select '[ABC]' from dual
      )
      select e.ex,
             case when e.ex like '[ABC]%' then 'MATCHES' else null end as matches_like,
             case when regexp_like(e.ex, '^[ABC].?') then 'MATCHES' else null end as matches_regexp_like,
             case when e.ex like 'A%' or e.ex like 'B%' or e.ex like 'C%' then 'MATCHES' else null end as matches_complex_like
        from examples e
      

      【讨论】:

        猜你喜欢
        • 2021-05-24
        • 1970-01-01
        • 2012-04-29
        • 1970-01-01
        • 2022-08-15
        • 1970-01-01
        • 2012-08-25
        • 2016-10-26
        • 2019-10-10
        相关资源
        最近更新 更多