让我们说这是我的表:
************
* film *
************
* title *
* film_id *
************
************
* actors *
************
* film_id *
* actor_id *
************
我想找到两位演员参与的电影的标题actor_id = 22 and actor_id = 23
怎么写查询?
我试过了:
SELECT title
FROM film
WHERE film_id IN(
SELECT film_id, COUNT(*)
FROM actors
WHERE actor_id=22 OR actor_id=23
GROUP BY film_id)
答案 0 :(得分:0)
我相信以下查询应该适合你。
SELECT title
FROM film f, actors a1, actors a2
WHERE f.film_id=a1.film_id AND a1.actor_id=22
AND f.film_id=a2.film_id AND a2.actor_id=23;
联接也是处理它的更好方法:
SELECT title
FROM film f
JOIN actors a1 ON f.film_id = a1.film_id
JOIN actors a2 ON f.film_id = a2.film_id
WHERE a1.actor_id = 22
AND a2.actor_id = 23;
答案 1 :(得分:0)
如果你内部加入演员表两次,并在每个连接中设置个人actor_id
作为标准,则需要两个演员都在电影中。因此,您只会返回同时拥有演员的电影。
SELECT film.title
FROM film
INNER JOIN actors actors1
ON film.film_id = actors1.film_id
AND actors1.actor_id=22
INNER JOIN actors actors2
ON film.film_id = actors2.film_id
AND actors2.actor_id=23
GROUP BY film.title