SQL:在子查询中选择MAX

时间:2013-09-11 14:39:12

标签: sql

好的,所以我的任务是找到一个特定演员的最新版本。有多个表,一个有基本的电影信息(标题,年份),一个有演员姓名,名字,Starid,另一个有他们演出的movienum,有Movienum,Starid。

基本上我已经获得了75%的路,而且我只想回复一部特定演员最新的电影。这是我目前的查询

  SELECT MovieTitle, Datereleased
  FROM MovieList
  WHERE Movienumber IN (SELECT Movienumber FROM ActsIn Where Starids IN (SELECT Starids FROM Stars       WHERE Familyname = 'Tarintino'))

现在,当我运行它时,我会获得3行3部电影及其相关信息。第一列是电影片名,第二列是Yearreleased。我该如何处理查询才能返回最新的电影? (例如MAX Yearreleased)

1 个答案:

答案 0 :(得分:4)

这是代码,现在研究它并查找你不知道的所有语法:

Select top 1 MovieTitle, DateReleased From MovieList
Inner Join
ActsIn
on ActsIn.MovieNumber = MovieList.MovieNumber
Inner Join
Stars
on Stars.Starids = ActsIn.StarIds
Where
Stars.FamilyName = 'Tarantino'
ORDER BY DateReleased DESC

这是另一种获取所有演员的所有最新版本列表的方法:

    select Stars.*, MovieTitle, DateReleased From MovieList inner join (
    Select MAX(DateReleased) AS DateReleased, StarIDs From MovieList
        Inner Join
        ActsIn
        on ActsIn.MovieNumber = MovieList.MovieNumber
    Group by StarIDs )  lastMovie on lastMovie.DateReleased = MovieList.DateReleased
    Inner join Stars on Stars.StarIDs = lastMovie.starIDs
Where FamilyName = 'Tarantino'