简体   繁体   English

为什么这个SQL查询失败了?

[英]Why is this SQL query failing?

Consider the following SQL: 考虑以下SQL:

SELECT mov
FROM movie
WHERE mov IN
    (
        SELECT mov
        FROM movie_star
        GROUP BY(mov)
        HAVING count(star) > 6
    )
INNER JOIN movie_star
ON movie_star.mov = movie.mov;

I am getting the following error when this query is executed: 执行此查询时出现以下错误:

.ERROR 1064 (42000): You have an error in your SQL syntax; .ERROR 1064(42000):您的SQL语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'inner join movie_star on movie_star.mov = movie.mov' at line 3 查看与您的MySQL服务器版本对应的手册,以便在第3行的movie_star.mov = movie.mov'上的'inner join movie_star'附近使用正确的语法

The JOIN has to go before the WHERE clause. JOIN必须在WHERE子句之前。

SELECT movie.mov
FROM movie
INNER JOIN movie_star ON movie_star.mov = movie.mov
WHERE mov IN (SELECT mov FROM movie_star GROUP BY mov HAVING COUNT(star) > 6);

Here's the same query cleaned up a little: 这是相同的查询清理了一点:

select mov
from movie
where
    mov in (select mov from movie_star group by(mov) having count(star) > 6)
inner join movie_star on movie_star.mov = movie.mov

You have a syntax error because an inner join appears after your where clause, when it should appear immediately after the from clause instead. 您有一个语法错误,因为inner join出现在您的where子句之后,而它应该出现在from子句之后。

Try this instead: 试试这个:

select mov
from movie
inner join movie_star on movie_star.mov = movie.mov
group by mov
having count(*) > 6

You've got the inner join in the wrong place. 你已经把内部联接放在了错误的地方。 Not sure I see the need for the inner join at all when you're only selecting the movie.mov column and the IN clause already guarantees that there are matching rows in movie_star . 当你只选择movie.mov列并且IN子句已经保证movie_star有匹配的行时,我不确定是否需要内连接。

select mov 
    from movie
        inner join movie_star 
            on movie_star.mov = movie.mov
    where mov in (select mov 
                      from movie_star 
                      group by(mov) 
                      having count(star)>6);

you have to join before you can use the where clause: 你必须先加入才能使用where子句:

select mov from movie
inner join movie_star on movie_star.mov = movie.mov
where mov in
(select mov from movie_star group by(mov) having count(star)>6)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM