如何在 MySQL LEFT JOIN 中使用别名

发布于 2024-12-10 03:00:17 字数 817 浏览 0 评论 0原文

我原来的查询是使用 WHERE 子句而不是 JOIN 进行连接。我意识到这并没有返回没有任何明星或类型的电影没有出现,所以我想我必须做一个 LEFT JOIN 才能显示每部电影。这是我原来的 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;

我尝试对电影进行 LEFT JOIN 我肯定做错了什么。

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;

我得到 ERROR 1054 (42S22): Unknown column 'sm.movi​​e_id' in 'on Clause' 很明显我做的连接是错误的,我只是不明白它是什么。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

水水月牙 2024-12-17 03:00:17

不要将逗号运算符与 JOIN 混合使用 - 它们具有不同的优先级! 手册中甚至有关于此的警告:

但是,逗号运算符的优先级低于 INNER JOIN、CROSS JOIN、LEFT JOIN 等。如果在存在连接条件时将逗号连接与其他连接类型混合使用,则可能会出现 'on Clause' 中的未知列 'col_name' 形式的错误。本节稍后给出有关处理此问题的信息。

试试这个:

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;

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;
简美 2024-12-17 03:00:17

您应该将与 JOIN 相关的条件放在同一个 ON 子句中。但是,对于上述问题,您应该使用以下查询:

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;

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;
匿名。 2024-12-17 03:00:17

人们警告说这是一次黑客攻击。

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;

连接右侧的表,其中包含您要比较的列。

People warn that this is a hack.

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;

Join with the right table, which has the columns you are comparing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文