PostgreSQL - Query 3 Tables

95 Views Asked by At

I have 3 tables: Table1,Table2,Table3 As shown below:

enter image description here

I will be filtering the Table3 based on the Value field. Example:

Input: xxx

Output is shown below:
Output should be based on the ID1 of Table1.

enter image description here

I query using below SQL query:

SELECT id, 
       id1, 
       id2, 
       value 
FROM   table1, 
       table2, 
       table3 
WHERE  ( table1.id1 = table3.id1 
         AND table2.id2 = table3.id2 ) 
       AND ( table3.value LIKE ? 
              OR table3.value ~ '[0-9]' )

Please give me a SQL query for this.

1

There are 1 best solutions below

0
On BEST ANSWER

Kindly find the SQl query:

select
table3.id, table3.id1, table3.id2, table3.value
from table3 
left join table1 on table3.id1=table1.id1
where
table1.id1 in (SELECT table3.id1 from table3 where table3.value="xxx")

Hopefully this works for you :)