MySql 查询问题子句顺序意外。 (在位置 117 的 "having" 附近)

MySql Query problem Unexpected ordering of clauses. (near "having" at position 117)

我必须将所有 SQL 查询转换为 MySql 以用于我的大学项目,但我 运行 遇到了以下查询的问题:

select a.actor_name, count(fa.actor_id) as Movies
from Actor a
    join Film_Actor fa on a.actor_id = fa.actor_id
having count(fa.actor_id) = (select distinct max(count(actor_id)) from Film_Actor group by actor_id)
group by a.actor_name;

上面写着:

Unexpected ordering of clauses. (near "having" at position 117)

我确定我在 MySql 代码中有语法问题(我们被教导没有 MySql 只有 Sql)

不能以这种方式嵌套聚合函数。试试这个:我们使用子查询,按计数排序,并将结果限制为仅 1 条记录,以获取一个演员参演的“最多”电影。

SELECT a.actor_name, count(fa.actor_id) as Movies
FROM Actor a
INNER JOIN  Film_Actor fa on a.actor_id = fa.actor_id
HAVING count(fa.actor_id) = (SELECT count(actor_id) 
                             FROM Film_Actor 
                             GROUP BY actor_id 
                             ORDER BY count(actor_id) DESC
                             LIMIT 1)
GROUP BYa.actor_name;

部分演示:https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=c511932c7a16f8c9f5ecb85f03ee21cb