Minaj
Minaj

Reputation: 175

ALias on an inner join intermediate result

My SQL query fails when I assign an alias to the result of a join.

Here is the example:

SELECT 
    a, b, c 
FROM 
    ((q 
INNER JOIN 
    r ON q.x = r.y) AS IntermediateResult 
INNER JOIN 
    s ON IntermediateResult.a = s.a)

Basically the alias seems not to be recognized in the second inner join.

I am using Mysql

Upvotes: 1

Views: 271

Answers (2)

Gordon Linoff
Gordon Linoff

Reputation: 1269613

You don't need a subquery at all:

SELECT a, b, c
FROM q INNER JOIN
     r
     ON q.x = r.y INNER JOIN
     s
     ON q.a = s.a;  -- This is either q.a or r.a

The parentheses are also unnecessary, unless you are using MS Access.

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726499

You do not have to label the results of an inner join as your intermediate result, because you can join directly to the table:

SELECT a,b,c FROM (
    SELECT * FROM q
    INNER JOIN r  AS IntermediateResult
            ON q.x=r.y
    INNER JOIN s
            ON IntermediateResult.a=s.a
) AS joined_qrs

Upvotes: 0

Related Questions