使用AND运算符无法获得正确的结果

时间:2016-04-24 20:23:23

标签: sql

我想知道什么是正确的查询来获取女演员X和Y的电影列表

DIAGRAM

我的查询(不起作用):

select m.movie_title
from movie m, actress a, actress_movie am
where m.movie_id = am.movie_id and am.actress_id = a.actress_id AND
a.actress_firstname = 'Jack' and a.actress_firstname = 'Kate' and a.actress_surname = 'Smith' and a.actress_surname = 'Connor'
group by m.movie_title;

1 个答案:

答案 0 :(得分:2)

你很亲密。但是,最重要的是学会使用正确的join语法。这实际上比回答这个问题更重要,这个问题只需要一个having条款:

select m.movie_title
from actress_movie am join
     movie m
     on m.movie_id = am.movie_id join
     actress a
     on am.actress_id = a.actress_id
where (a.actress_firstname = 'Jack' and a.actress_surname = 'Connor') or
      (a.actress_firstname = 'Kate' and a.actress_surname = 'Smith')
group by m.movie_title
having count(distinct am.actress_id) = 2;

作为一个注释:有一个标有" actress"的表格似乎很奇怪。在基于性别的数据库中分离这些人似乎是一种糟糕的数据模型。