所以我有3个表,演员(id,姓名),电影(id,姓名,年份)和演员表(援助,中期)(演员身份和电影ID)。我的目标是选择在1900年以前拍摄过电影的所有演员以及2000年以后的电影。 我的问题是
select a.id
from actor a, movie m1, casts c1, movie m2, casts c2
where a.id = c1.aid = c2.aid and c1.mid = m1.id and c2.mid = m2.id and
m1.year >2000 and m2.year <1900;
这个查询花了很长时间,似乎没有产生正确的结果。 那么有人可以帮我吗?
答案 0 :(得分:2)
要获得两个日期范围内的电影演员,请使用两个子查询。像这样:
select yourFields
from yourTables
where actorId in (subquery to get actor id's for one date range)
and actorId in (subquery to get actor id's for second date range)
你可以弄清楚细节。
答案 1 :(得分:1)
我认为问题是表达式a.id = c1.aid = c2.aid
。如果我没有弄错的话,首先将c1.aid
与c2.aid
进行比较,然后将布尔结果与a.id
进行比较。
你可以试试这个:
select a.id
from actor a
inner join casts c1 on c1.aid = a.id
inner join casts c2 on c2.aid = a.id
inner join movie m1 on c1.mid = m1.id
inner join movie m2 on c2.mid = m2.id
where m1.year >2000 and m2.year <1900;
或者,如果您更喜欢内部联接的where
语法,只需将a.id = c1.aid = c2.aid
更改为a.id = c1.aid and a.id = c2.aid
答案 2 :(得分:1)
这也行
{{1}}
答案 3 :(得分:0)
由于表的大小和大量连接,此查询可能需要很长时间才能运行。
此查询返回结果,因为数据库中存在错误。
正确的查询是:
SELECT DISTINCT a1.fname, a2.lname
FROM
-- Create the table used to get all movies before 1900
actor AS a1
INNER JOIN casts AS c1
ON a1.id=c1.pid
INNER JOIN movie as m1
on m1.id = c1.mid,
-- Create the table used to get all movies after 2000
actor as a2
INNER JOIN casts AS c2
ON a2.id=c2.pid
INNER JOIN movie as m2
on m2.id = c2.mid
-- Only display actors that have played before 1900 and after 2000
WHERE m1.year < 1900 AND m2.year > 2000 AND a1.id = a2.id;