【问题标题】:Search the value from one column in another column in a different table从不同表的另一列中的一列中搜索值
【发布时间】:2013-02-20 21:58:19
【问题描述】:

我需要用另一个表的列值从一个表中搜索列值。

例如

  MyTable
    Col1 Col2
    AAA   1
    BBB   2
    CCC   3

  MyTable2
    Col1          Col2
    GHKGH AAAh      1
    dhsjsBvd        2
    bdnd CCC b      3

我需要在 MyTable2 的 col1 值中搜索 MyTable 中的 col1 值。

我不想对字符串进行硬编码,而是从表中获取值。

尝试使用instrregex_instr,但这些函数不允许搜索模式中的列值。

我正在使用 oracle 10g。 TIA

【问题讨论】:

    标签: sql oracle oracle10g


    【解决方案1】:

    此处测试示例:http://sqlfiddle.com/#!4/037ffe/3

    select 
      t1.col1 AS t1col1, t2.col1 AS t2col1
    from 
      MyTable t1
    
      left join MyTable2 t2
      on t2.col1 like '%' || t1.col1 || '%'
    

    包含 DDL 的完整示例:

    CREATE TABLE MyTable (col1 varchar2(9));
    
    INSERT ALL 
        INTO MyTable (col1)
             VALUES ('AAA')
        INTO MyTable (col1)
             VALUES ('BBB')
        INTO MyTable (col1)
             VALUES ('CCC')
    SELECT * FROM dual;
    
    CREATE TABLE MyTable2 (col1 varchar2(18));
    
    INSERT ALL 
        INTO MyTable2 (col1)
             VALUES ('GHKGH AAAh')
        INTO MyTable2 (col1)
             VALUES ('dhsjsBvd')
        INTO MyTable2 (col1)
             VALUES ('bdnd CCC b')
    SELECT * FROM dual;
    
    select 
      t1.col1 AS t1col1, t2.col1 AS t2col1
    from 
      MyTable t1
    
      left join MyTable2 t2
      on t2.col1 like '%' || t1.col1 || '%'
    

    结果集:

    T1COL1 T2COL1 AAA GHKGH AAAh BBB(空) CCC bdnd CCC b

    【讨论】:

      猜你喜欢
      • 2015-02-15
      • 1970-01-01
      • 2022-01-21
      • 2020-12-13
      • 2021-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多