Col*_*een 5 sql sql-server-2008
我有(来自 imdb 数据库)一张casts表和一张movie表。我正在尝试查找演员阵容最多(不同演员)的电影的名称和演员阵容。
这是我想出的,但我得到了
“totalcount”出现“无效列名”错误
为什么?
另外,这个查询看起来正确吗?
select name, count(distinct pid) as totalcount
from casts join movie on mid=movie.id
where totalcount =
(select max(CastCount.total)
from (select count(distinct pid) as total from casts group by mid)CastCount)
group by name;
Run Code Online (Sandbox Code Playgroud)
您不能在WHERE. 由于它是聚合的,因此您应该将其替换为HAVING:
HAVING totalcount = ...
另外,您的子查询可能返回超过 1 行,并且您将遇到另一个错误。