0

Im trying to find a list of all movies that that contain 2 or MORE cast members in them.

select title from Movie where movieID in(
    select movieID
    from Cast
    join MovieHasCast
        on Cast.castID = MovieHasCast.castID
    where cast.name = 'gene wilder'
intersect
    select movieID
    from Cast
    join MovieHasCast
        on Cast.castID = MovieHasCast.castID
    where cast.name = 'richard pryor')

my alternative would be something like this:

select title from Movie where movieID in(
    select movieID
    from Cast
    join MovieHasCast
        on Cast.castID = MovieHasCast.castID
    where cast.name = 'gene wilder'
    and movieID in (
        select movieID
        from Cast
        join MovieHasCast
            on Cast.castID = MovieHasCast.castID
        where cast.name = 'richard pryor'))

This is just a sample of how I am accomplishing this. However, I am building this query dynamically in C# to filter a list of movies by genre, cast members, etc. I am concerned with performance.

Is this the best way to accomplish this task? I feel like there is a better way to do this I just don't know how especially since a User could select 10 cast members and that query would start to get really large - even though it would likely return 0 records.

4

1 回答 1

1

尝试

select movie.name
from movie
join moviehascast mc on mc.movieid = movie.id
join cast on cast.id = mc.castid
where cast.name in (@actor1, @actor2)
group by movie.name
having count(1) = @numberOfActorsSearched
于 2013-10-27T01:18:38.703 回答