0

问题:有多少部电影有最少数量的演员?

桌子:

title | year | person | role (actor, director, writer, producer)

我做了什么:

SELECT title, count(role) 
FROM movie_table 
where role='actor' 
GROUP BY title 
ORDER by count(role) ASC;

我得到了什么:

title (007, Dark Knight, Superman, Batman ...) | count(role) (1, 1, 2, 2,...)

我需要的是一种计算具有最小演员角色的电影的方法,在这种情况下是 2(007 和黑暗骑士)。

4

1 回答 1

1

目前还不清楚你想要什么,但似乎你在问这个:

SELECT title, count(role) 
FROM movie_table 
where role='actor' 
GROUP BY title 
HAVING count(role) = 1
ORDER by count(role) ASC;

根据您的评论,您可以使用以下内容:

SELECT title
FROM movie_table 
where role='actor' 
GROUP BY title 
HAVING count(role) = (select min(cnt)
                      from (select count(role) cnt
                            from movie_table
                            group by title) c)
ORDER BY count(role);

请参阅带有演示的 SQL Fiddle

如果您只想要总数,请将count()函数应用于查询:

select count(title) Total
from
(
  SELECT title
  FROM movie_table 
  where role='actor' 
  GROUP BY title 
  HAVING count(role) = (select min(cnt)
                        from (select count(role) cnt
                              from movie_table
                              group by title) c)
) x

SQL Fiddle with Demo

于 2012-10-23T18:25:04.133 回答