Search code examples
sqlplsqloracle-sqldeveloper

Query to get specific value between two table


I have two table which contains two different primary key, lets call them, table1 and table2.

The tables may have the same number of columns.

Table1:

ID NOM CODE
1 AAA 661YYYDD
2 BBB YYYD661
3 CCC YD661
4 DDD P5500Z

Table 2:

ID KEYCODE
1 661
2 55

I want to be able to get by KEYCODE: ALL record in table1 which contain 661 or 55. For example when I select by 661 I get only the first 3 rows from tables1.


Solution

  • This works as well:

    SELECT *
    FROM TABLE1
    JOIN TABLE2
    ON TABLE1.CODE LIKE '%'||TABLE2.KEYCODE||'%'
    WHERE TABLE2.KEYCODE = '661'
    

    dbfiddle