获取仅属于一个类别的所有值

时间:2013-10-13 12:28:15

标签: sql oracle

我有以下架构。

MovieInfo (mvID, title, rating, year, length, studio)
DirectorInfo(directorID, firstname, lastname)
DirectInfo(mvID*, directorID*)
GenreInfo(mvID*, genre)

我希望输出仅在1种类型中工作的演员的详细信息列表,例如动作电影。电影到流派是一对多的关系。

到目前为止我有这个但是我遇到了错误。

SELECT ActorInfo.ActorID, ActorInfo.firstname, ActorInfo.lastname
FROM ActorInfo, GenreInfo, CastInfo
WHERE CastInfo.mvID = GenreInfo.mvID
AND CastInfo.ActorID = ActorInfo.ActorID
AND Genreinfo.genre = 'ACtion'
MINUS
(SELECT ActorInfo.ActorID, ActorInfo.firstname, ActorInfo.lastname,
FROM ActorInfo, GenreInfo, CastInfo
WHERE CastInfo.mvID = GenreInfo.mvID
AND CastInfo.ActorID = ActorInfo.ActorID
AND Genreinfo.genre != 'Action')
GROUP BY ActorInfo.ActorID, ActorInfo.firstname, ActorInfo.lastname, 

2 个答案:

答案 0 :(得分:0)

也许这样:

select
        ActorInfo.ActorID, ActorInfo.firstname, ActorInfo.lastname, max(genre)
from
        ActorInfo,
        GenreInfo,
        CastInfo
where
        CastInfo.ActorID = ActorInfo.ActorID and
        CastInfo.mvID = GenreInfo.mvID
group by 
        ActorInfo.ActorID, ActorInfo.firstname, ActorInfo.lastname
having
        count(distinct genre) =1 and
        max(genre) = 'Action'

答案 1 :(得分:0)

我看到你在第二个查询的SELECT列表中缺少COUNT(*)。另外,为什么括号中的第二个查询?为什么GROUP BY子句在查询之外?

问题是,在第一个查询中,您有ACtion字符串,而不是Action - 这是不一样的。

试试这个:

SELECT ActorInfo.ActorID, ActorInfo.firstname, ActorInfo.lastname
  FROM ActorInfo, GenreInfo, CastInfo
WHERE CastInfo.mvID = GenreInfo.mvID
  AND CastInfo.ActorID = ActorInfo.ActorID
  AND Genreinfo.genre = 'Action'
MINUS
SELECT ActorInfo.ActorID, ActorInfo.firstname, ActorInfo.lastname
  FROM ActorInfo, GenreInfo, CastInfo
WHERE CastInfo.mvID = GenreInfo.mvID
  AND CastInfo.ActorID = ActorInfo.ActorID
  AND Genreinfo.genre != 'Action'
GROUP BY ActorInfo.ActorID, ActorInfo.firstname, ActorInfo.lastname;