user3200143
user3200143

Reputation:

trouble with postgresql subquery

Here are my tables:

table1:

ID | data1 | data2
1  | xxx   | xxx 
2  | xxx   | xxx

table2:

ID | table1_id
20 | 1
21 | 1
25 | 2
26 | 2

table3:

ID | table2_id
30 | 20
31 | 21
32 | 25
33 | 26 <--

I have marked the relevant row by an arrow (table3: ID=33 | table2_id=26)

Now, I want the matching ID with data 1 and data 2 from table 1. In this case: 2

I tried something ...

SELECT t1."ID"
FROM table AS t1

INNER JOIN table2 AS tb2
ON t1."ID" = t2."ID"

INNER JOIN table3 AS t3
ON t2."ID" = 26

... but it returns nothing. Have anybody a working subquery for me :)

Upvotes: 0

Views: 67

Answers (1)

Rapha&#235;l Althaus
Rapha&#235;l Althaus

Reputation: 60493

you join on the wrong fields

SELECT t1."ID"
FROM table AS t1

INNER JOIN table2 AS t2
   ON t1."ID" = t2.table1_id

INNER JOIN table3 AS t3
   ON t2."ID" = t3.table2_id
WHERE t3."ID" = 33

Upvotes: 1

Related Questions