我即将参加数据库考试,但我不知道如何进行此查询。
我按原样复制练习: 给定2张桌子
FILM(Title, Director, Year, Genre)
InCastOf(Actor, Film) Where InCastOf(film) is constrained to Film(Title)
编写查询以查找在同一部电影中完全扮演过的所有演员。
到目前为止,我得到的是:
SELECT InCastOf.Actor AS A1, T.Actor AS A2, InCastOf.Film FROM InCastOf
JOIN (SELECT Actor, Film FROM InCastOf) AS T ON InCastOf.Film = T.Film
WHERE InCastOf.Actor != T.Actor
对于每部电影,哪位演员都给我带来了回报?我可以使用group by等来简化查询,但是“准确”一词给我带来了一些麻烦,因为我需要确保这些演员只在同一部电影上工作,而没有其他工作。至少我从“完全”一词中了解到这一点。
感谢您的帮助!
答案 0 :(得分:3)
由于@Martin Smith的提示,我将查询编写如下:
SELECT T1.Actor AS A1, T2.Actor AS A2, T1.films FROM (
SELECT Actor, GROUP_CONCAT(Film) AS films
FROM InCastOf
GROUP BY Actor) AS T1
JOIN (
SELECT Actor, GROUP_CONCAT(Film) AS films
FROM InCastOf
GROUP BY Actor) AS T2
ON T1.films = T2.films
WHERE T1.Actor > T2.Actor