我正在尝试找到正确的SQL来执行我的任务。我试图找到所有演员都在同一部电影中扮演的人数。对于所有的电影。我获取电影片名的SQL语句以及每个演员的名字和姓氏是。
SELECT title, actor.first_name, actor.last_name,
FROM film
INNER JOIN film_actor ON film.film_id = film_actor.film_id
INNER JOIN actor ON film_actor.actor_id = actor.actor_id
ORDER BY film.title
我的数据库返回:
正如你所看到的,有10个演员属于电影名称ACADEMY DINOSAUR。什么是能为我提供每部电影的演员的SQL?
答案 0 :(得分:2)
你的SQL可以是这样的
SELECT title, count(1) COUNT
FROM film
INNER JOIN film_actor ON film.film_id = film_actor.film_id
INNER JOIN actor ON film_actor.actor_id = actor.actor_id
GROUP BY title
答案 1 :(得分:1)
请试试这个
select tt.title, count(1) from (
SELECT title, actor.first_name, actor.last_name,
FROM film
INNER JOIN film_actor ON film.film_id = film_actor.film_id
INNER JOIN actor ON film_actor.actor_id = actor.actor_id
) tt group by tt.title
ORDER BY tt.title