mysql - How to select records in a many-to-many relation using an AND condition -
this question has answer here:
i have db tracks movies , actors, following tables:
person(id, name) movie(id, name) actors(id, personid, movieid)
id fields primary keys , personid & movieid foreign keys.
i want select movies tom cruise , brad pitt played in. tried following:
select * person, actors person.id = actors.actorid , (person.name= 'tom cruise' or person.name= 'brad pitt') group actors.movieid having count(actors.movieid) > 1
this doesn't work, because person name not unique (and can't change unique). if have actor named brad pitt , 2 brad pitts played in same movie return result too.
how can it?
note: number of actors querying can change. might need movie 10 actors played in it.
do inner join between tables below
select m.name moviename movie m inner join actors on m.id = a.movieid inner join person p on p.id = a.actorid , p.name in ('tom cruise','brad pitt')
Comments
Post a Comment