2013-02-20 158 views
1

我需要從一個表中搜索具有另一個表的列值的列值。在另一個表中的另一列中搜索一列中的值

例如

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

回答

4

測試這裏的例子: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  (null) 
CCC  bdnd CCC b 
+0

的偉大工程...感謝 – Stu 2013-02-20 22:20:18

+0

你最受歡迎的。 – bernie 2013-02-20 22:21:22

相關問題