0

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 :)

4
  • I'm sure that the questions on SO used to be more intellectually challenging. Commented Jan 15, 2014 at 22:04
  • you've tagged this with multiple dbs, but in mysql t1."ID" and the like would NOT be a valid table/field identifier. it'd be t1.ID only, without the quotes. You could very well have syntax errors that your containing code is not checking for. Commented Jan 15, 2014 at 22:04
  • by the way, you join on the wrong field : t1.Id = t2.table1_id not t2.id for the first join. For the third, you probably mean on t2.id = t3.table2_id Commented Jan 15, 2014 at 22:05
  • @MarcB: t1."ID" is a standard compliant (valid) column identifier - even in MySQL: sqlfiddle.com/#!2/cdd8e/2 Commented Jan 15, 2014 at 22:07

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

Plus, a typo in the table alias tb2. --> t2. And the now missing WHERE condition: WHERE t3."ID" = 33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.