Oracle
 sql >> Database >  >> RDS >> Oracle

Oracle Query:come confrontare due colonne di tabelle diverse con valori separati da virgole

Puoi ottenerlo usando TRANSLATE e TRIM funzionare come segue.

SQL> with t1 as
  2  (select 1 as product_id, 'O,G,E,H,R' as prod_name from dual union all
  3  select 2, 'P,D,H,P,N' from dual union all
  4  select 3, 'C,D,A,D,P' from dual union all
  5  select 4, 'E,D,A,D,P' from dual),
  6  t2 as
  7  (select 5 as product_id, 'R,O,G,E,H' as prod_name from dual union all
  8  select 6 as product_id, 'P,D,H,N,P' as prod_name from dual union all
  9  select 7 as product_id, 'C,D,A,D' as prod_name from dual union all
 10  select 8 as product_id, 'C,D,A,P,D' as prod_name from dual)
 11  SELECT
 12      T1.PRODUCT_ID,
 13      T2.PRODUCT_ID
 14  FROM
 15      T1
 16      JOIN T2 ON TRIM('#' FROM TRANSLATE(T1.PROD_NAME, T2.PROD_NAME, '#')) IS NULL;

PRODUCT_ID PRODUCT_ID
---------- ----------
         1          5
         2          6
         3          8

SQL>

Questa query considera che il tuo nome_prod non ha # carattere in uno qualsiasi dei valori.

Ciao!!