Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using left join and inner join in the same query

Below is my query using a left join that works as expected. What I want to do is add another table filter this query ever further but having trouble doing so. I will call this new table table_3 and want to add where table_3.rwykey = runways_updatable.rwykey. Any help would be very much appreciated.

SELECT *  FROM RUNWAYS_UPDATABLE  LEFT JOIN TURN_UPDATABLE  ON RUNWAYS_UPDATABLE.RWYKEY = TURN_UPDATABLE.RWYKEY  WHERE RUNWAYS_UPDATABLE.ICAO = 'ICAO' AND (RUNWAYS_UPDATABLE.TORA > 4000 OR LDA > 0)  AND (TURN_UPDATABLE.AIRLINE_CODE IS NULL OR TURN_UPDATABLE.AIRLINE_CODE = ''  OR TURN_UPDATABLE.AIRLINE_CODE = '')  

'*************EDIT To CLARIFY ***************** Here is the other statement that inner join i would like to use and I would like to combine these 2 statements.

SELECT *  FROM RUNWAYS_UPDATABLE A, RUNWAYS_TABLE B WHERE A.RWYKEY = B.RWYKEY 

'***What I have so far as advice taken below, but getting syntax error

     SELECT RUNWAYS_UPDATABLE.*, TURN_UPDATABLE.*,  AIRPORT_RUNWAYS_SELECTED.*       FROM RUNWAYS_UPDATABLE        INNER JOIN  AIRPORT_RUNWAYS_SELECTED            ON RUNWAYS_UPDATABLE.RWYKEY = AIRPORT_RUNWAYS_SELECTED.RWYKEY      LEFT JOIN TURN_UPDATABLE           ON RUNWAYS_UPDATABLE.RWYKEY = TURN_UPDATABLE.RWYKEY  

NOTE: If i comment out the inner join and leave the left join or vice versa, it works but when I have both of joins in the query, thats when im getting the syntax error.

like image 997
Will Avatar asked Mar 13 '12 13:03

Will


People also ask

Can I use inner join and left join together?

You'll use INNER JOIN when you want to return only records having pair on both sides, and you'll use LEFT JOIN when you need all records from the “left” table, no matter if they have pair in the “right” table or not.

Can we use two different joins in same query?

Multiple joins can be described as a query containing joins of the same or different types used more than once, thus giving them the ability to combine multiple tables. For this article we will first create a database geeks and then create three tables in it and then run our queries on those tables.

Can we use both inner join and outer join in a single query?

Yes you can do both is the same query, and yes the order is important.

Can we use left and right join in same query?

The only you can do that is by using UNION .


2 Answers

I always come across this question when searching for how to make LEFT JOIN depend on a further INNER JOIN. Here is an example for what I am searching when I am searching for "using LEFT JOIN and INNER JOIN in the same query":

SELECT * FROM foo f1 LEFT JOIN (bar b1   INNER JOIN baz b2 ON b2.id = b1.baz_id ) ON   b1.id = f1.bar_id 

In this example, b1 will only be included if b2 is also found.

like image 189
Gajus Avatar answered Sep 21 '22 02:09

Gajus


Remember that filtering a right-side table in left join should be done in join itself.

select * from table1    left join table2     on table1.FK_table2 = table2.id     and table2.class = 'HIGH' 
like image 24
Nikola Markovinović Avatar answered Sep 22 '22 02:09

Nikola Markovinović