【问题标题】:Oracle Get Records where String not present as Sub string in other rows?Oracle 获取记录,其中字符串不作为子字符串出现在其他行中?
【发布时间】:2019-05-25 18:22:00
【问题描述】:

嗨,这是我的问题:-

Value
-----
ABCDE
ABCD
ABC
ABF
ABFG

结果应该是:ABCDE & ABFG

上面不是任何其他字符串的子字符串,在同一列中没有重复

【问题讨论】:

    标签: sql oracle plsql substring


    【解决方案1】:

    你可以这样做:

    select *
    from my_table t1
    where not exists (
      select 1 from my_table t2
      where t1.value <> t2.value 
        and t2.value like '%' || t1.value || '%'
    )
    

    【讨论】:

      【解决方案2】:

      这可以通过 LEFT JOIN 结合 WHERE .., IS NULL 来实现。

      与关联查询解决方案相比,LEFT JOIN 查询具有更短/更简洁的语法,并且在处理大型数据集时通常表现更好。

      SELECT t1.value
      FROM 
          my_table t1
          LEFT JOIN my_table t2 
              ON t2.value <> t1.value 
              AND t2.value LIKE '%' || t1.value || '%'
      WHERE t2.value IS NULL
      

      【讨论】:

        猜你喜欢
        • 2012-08-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-06-04
        • 1970-01-01
        • 2020-04-10
        • 1970-01-01
        相关资源
        最近更新 更多