Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use aliases with MySQL LEFT JOIN

My original query is doing joins using the WHERE clause rather than JOIN. I realized that this was not returning movies that did not have any stars or genres did not show up so I think I have to do a LEFT JOIN in order to show every movie. Here is my original SQL:

SELECT * FROM movies m, stars s, stars_in_movies sm, genres g, genres_in_movies gm WHERE m.id = sm.movie_id AND sm.star_id = s.id AND gm.genre_id = g.id AND gm.movie_id = m.id AND m.title LIKE '%the%' AND s.first_name LIKE '%Ben%' ORDER BY m.title ASC LIMIT 5; 

I tried to do a LEFT JOIN on movies I'm definitely doing something wrong.

SELECT * FROM movies m, stars s, stars_in_movies sm, genres g, genres_in_movies gm LEFT JOIN movies m1 ON m1.id = sm.movie_id LEFT JOIN movies m2 ON m2.id = gm.movie_id AND sm.star_id = s.id AND gm.genre_id = g.id ORDER BY m.title ASC LIMIT 5; 

I get ERROR 1054 (42S22): Unknown column 'sm.movie_id' in 'on clause' so clearly I'm doing the join wrong, I just don't see what it is.

like image 937
styfle Avatar asked Oct 15 '11 21:10

styfle


People also ask

Can we use alias in left join?

Rather than using the table names, you may also use table alias as specifying the tables in the LEFT OUTER JOIN query. LEFT JOIN sto_orders ORDs ON EMPs.id=ORDs. emp_id; The same result is retrieved as in case of the first example.

Can you use alias in join SQL?

SQL aliases are custom names that you can give to the columns and tables you include in your queries. Aliases are very useful, as they can greatly improve the readability and maintainability of your query.

Can you use column alias in join?

No, you cannot do that. The alias is not bound until later in the processing. You can use "Nombre" in an ORDER BY, but not in a WHERE clause and certainly not in a JOIN clause.


2 Answers

Don't mix the comma operator with JOIN - they have different precedence! There is even a warning about this in the manual:

However, the precedence of the comma operator is less than of INNER JOIN, CROSS JOIN, LEFT JOIN, and so on. If you mix comma joins with the other join types when there is a join condition, an error of the form Unknown column 'col_name' in 'on clause' may occur. Information about dealing with this problem is given later in this section.

Try this instead:

SELECT * FROM movies m LEFT JOIN (    stars s    JOIN stars_in_movies sm        ON sm.star_id = s.id ) ON m.id = sm.movie_id AND s.first_name LIKE '%Ben%' LEFT JOIN (     genres g     JOIN genres_in_movies gm         ON gm.genre_id = g.id ) ON gm.movie_id = m.id WHERE m.title LIKE '%the%' ORDER BY m.title ASC LIMIT 5; 
like image 188
Mark Byers Avatar answered Sep 21 '22 23:09

Mark Byers


You should put your conditions related to your JOINs in the same ON clause. However, for your above problem, you should use the following query:

SELECT * FROM movies m  LEFT JOIN stars_in_movies sm ON sm.movie_id = m.id JOIN stars s ON sm.star_id = s.id LEFT JOIN genres_in_movies gm ON gm.movie_id = m.id JOIN genres g ON gm.genre_id = g.id ORDER BY m.title ASC LIMIT 5; 
like image 34
Chris J Avatar answered Sep 22 '22 23:09

Chris J