0

I've got three database tables that are connected trough each other but not directly. Here are the tables:

Table one
-----------
id
rank
table_two_id

Table two
-----------
id
amount
table_three_id

Table three
-----------
id
name

So each row in table one can be directly linked to the row in the third table. I need to get some columns from table_one plus the name column from the third column.

I've tried many combinations so far, but this one seems to be the most promising so far, however I can't get it to work :

    select (select s.table_three_id 
            from table_two s 
            where s.id = r.table_two_id) as three_id, 
    table_three.name 
    from table_one r
    INNER JOIN table_three ON (three_id = table_three.id);

I get an error saying column three_id doesn't exist. How do I get the info from the third table based on key from table_one trough table_two.

2
  • None of your tables has a column named three_id ... Commented May 22, 2013 at 12:11
  • and none of you tables is named table_three Commented May 22, 2013 at 12:15

1 Answer 1

1

It should be this way:

select 
 r.*,
 table_three.name 
from table_one r
INNER JOIN table_two s ON s.id           = r.table_two_id
INNER JOIN table_three ON table_three.id = s.table_three.id;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.