Home » Mysql » How to use aliases with MySQL LEFT JOIN

How to use aliases with MySQL LEFT JOIN

Posted by: admin November 30, 2017 Leave a comment

Questions:

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.

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;

Questions:
Answers:

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;

Questions:
Answers:

Maybe ugly, But the way it will work is here. Beware this is ugly and lot of people is giving warning about this kind of hacks

SELECT *
FROM movies m, stars_in_movies sm LEFT JOIN movies m1 ON m1.id = sm.movie_id, stars s
ORDER BY m.title ASC
LIMIT 5;

when using joins, you must do the join with the right table which have the columns you are comparing.

Questions:
Answers:

SQL Join (inner join in MySQL)

select emp1.id,emp1.name,emp1.job  from (select id, type as name, description as job from component_type as emp1)emp1
inner join 
emp
on emp1.id=emp.id;

Left Join

select emp1.id,emp1.name,emp1.job  from (select id, type as name, description as job from component_type as emp1 where id between '1' AND '5')emp1
left join 
emp
on emp1.id=emp.id;

Right Join

select emp1.id,emp1.name,emp1.job  from (select id, type as name, description as job from component_type as emp1)emp1
Right join 
(select * from emp where id between '1' and '5')exe
on emp1.id=exe.id;

Questions:
Answers:

Using alias connect many table without using join..

select sum(s.salary_amount) as total_expenses_paid_to_all_department

from salary_mas_tbl s,dept_mas_tbl d

where s.salary_dept=d.dept_id;