mysql查询找到演员在两部电影中扮演的角色

时间:2018-03-18 14:40:16

标签: mysql sql

我试图展示演员在这两部电影中扮演的角色 请帮助,它没有得到

SELECT actors.first_name,actors.last_name
FROM actors
LEFT JOIN films_actors
  ON actors.actor_id=films_actors.actor_id
LEFT JOIN films
  ON films_actors.film_id=films.film_id
WHERE films.title="ACADEMY DINOSAUR"
  AND films.title="ANACONDA CONFESSIONS"

1 个答案:

答案 0 :(得分:3)

您可以使用HAVING COUNT(DISTINCT ...)

SELECT actors.first_name,actors.last_name 
FROM actors 
JOIN films_actors 
  ON actors.actor_id=films_actors.actor_id
JOIN films 
  ON films_actors.film_id=films.film_id 
WHERE films.title IN ('ACADEMY DINOSAUR','ANACONDA CONFESSIONS')
GROUP BY actors.first_name,actors.last_name 
HAVING COUNT(DISTINCT films.title) = 2;