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