Let's start from the inner query and work our way out. You want to return rows from Table3
for which a column matches (using the LIKE
operator) any column from Table1
, right? I would use the EXISTS operator for that. We can reword your problem as "return all rows from table3
for which there exists at least one matching row from Table1
". Here's one implementation:
SELECT key_in_table
FROM Table3
WHERE EXISTS
(
SELECT 1
FROM Table1
WHERE UPPER(Table3.some_column) LIKE '%' || Table1.column_name || '%'
);
You can run that query separately so you can be satisfied that it returns the results that you're after. The final query is as simple as integrating that subquery into what you already had:
SELECT dif_col_1, dif_col_2
FROM Table2
WHERE key_in_table =
(
SELECT key_in_table
FROM Table3
WHERE EXISTS
(
SELECT 1
FROM Table1
WHERE UPPER(Table3.some_column) LIKE '%' || Table1.column_name || '%'
)
);
...Where upper(some_column) in (select distinct upper(column_name) from table1);
?DISTINCT
is NOT a function. Writingdistinct (column_name)
is exactly the same thing asdistinct column_name