Can I join 2 tables the rename the join using `AS` in order to join with yet a third table?

StackOverflow https://stackoverflow.com/questions/23685841

  •  23-07-2023
  •  | 
  •  

سؤال

Can I do something as below? The reason I need to do this is because table 3 has a different column name for ID.

select *
from (table1 join table2 using(ID)) as result 
join table3 
on result.ID = table3.different_ID

Currently I'm having a syntax error, but my query is rather complex so I'm not sure whether this is the problem or something else.

هل كانت مفيدة؟

المحلول

Having you considered just using on?

select *
from table1 join
     table2
     on table1.id = table2.id join 
     table3 
     on table1.ID = table3.different_ID;

نصائح أخرى

You'll need to do something like this:

SELECT *
FROM (
    SELECT *
    FROM table1 JOIN table2 USING (ID)
) result
JOIN table3 ON result.ID = table3.different_ID;

EDIT: Updated to reflect OP's original intent and fit OP's needs considering query complexity

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top