用于查找在2002,2003或2005年拍摄的电影名称的查询

时间:2016-01-19 10:23:27

标签: sql ms-access

鉴于该网站的数据库" eMovies"管理有关电影电影的数据。

  

写入SQL查询,查找在2002年,2003年或2005年拍摄的电影的名称,其姓氏为"福特"谁在那些电影中播放过。

enter image description here

表格:

Actors 
    ActorID      PK
    Firstname 
    LastName

Directors 
    DirectorID   PK
    Firstname 
    LastName

Genres 
    GenreID      PK
    GenreDescr 

Movies 
    MovieID      PK
    MovieName 
    Year

ActorsMovies 
    MovieID      PK
    ActorID      PK 

DirectorsMovies 
    MovieID      PK
    DirectorID   PK 

GenresMovies 
    MovieID      PK
    GenreID      PK 

我的尝试:

 SELECT Movies.MovieName
 FROM Movies,Actors,ActorsMovies
 WHERE Movies.Year=2002 OR Movies.Year=2003 OR Movies.Year=2005
 AND Actors.LastName = "Ford";

输出:

enter image description here

2 个答案:

答案 0 :(得分:1)

加入你的桌子。

SELECT m.MovieName
FROM Movies m
INNER JOIN ActorsMovies am ON m.movieID = am.movieID
INNER JOIN Actors a ON am.ActorID = a.ActorID
WHERE m.Year IN ('2002', '2003', '2005')
AND a.LastName = 'Ford';

如果您正在寻找更广泛的演员姓名搜索,请使用..

SELECT m.MovieName
FROM Movies m
INNER JOIN ActorsMovies am ON m.movieID = am.movieID
INNER JOIN Actors a ON am.ActorID = a.ActorID
WHERE m.Year IN ('2002', '2003', '2005')
AND (a.LastName = 'Ford' OR a.FirstName = 'Ford');

甚至更广泛..

SELECT m.MovieName
FROM Movies m
INNER JOIN ActorsMovies am ON m.movieID = am.movieID
INNER JOIN Actors a ON am.ActorID = a.ActorID
WHERE m.Year IN ('2002', '2003', '2005')
AND (a.LastName LIKE '%Ford%' OR a.FirstName LIKE '%Ford%');

答案 1 :(得分:0)

SELECT Movies.MovieName
  FROM Movies,Actors,ActorsMovies
 WHERE Movies.MovieID = Actors.MovieID
   AND Actors.ActorID = ActorsMovies.ActorID
   AND Movies.Year in (2002,2003,2005)
   AND Actors.LastName = "Ford";

您没有提供任何加入条件,因此您获得了所有结果。 此外,使用IN代替或将同一列与不同结果进行比较时。 最后,不要使用此连接,学习内连接的正确语法,这将有助于您避免将来出现这些错误。

select * from TABLE1 INNER JOIN TABLE2 on(table1.COLUMN = TABLE2.COLUMN)