简单的SQLite查询

时间:2013-10-14 23:04:20

标签: sqlite relational-database

我如何查询这4个表以查看 Apocalypse Now 中的演员?

Studio Table
studioID  studioName
--------  --------
1         Lucasfilm
2         Amblin Entertainment
3         Zoetrope Studios

Movie Table
movieID   movieName        studioID    castID
--------  --------        --------    --------
1         Star Wars            1          1
2         Indiana Jones        1          2
3         Back to the Future   2          3
4         Apocalypse Now       3          4

Cast Table
castID   movieID  actorID  roleName
-------- -------- -------- --------
1          1         1     Han Solo
2          2         1     Indiana Jones
3          3         3     Doc Brown
4          4         1     Colonel Lucas
5          4         2     Tyrone Miller

Actor Table
actorID  lastName     firstName
-------- --------     --------
1        Ford         Harrison
2        Fishburne    Laurence
3        Lloyd        Christopher

现在我在想:

SELECT Studio.studioName, Movie.movieName, Cast.roleName, Actor.firstName, Actor.lastname
FROM Studio, Movie, Actor, Cast 

WHERE Cast.castID = Movie.castID and Movie.movieName = 'Apocalypse Now'

ORDER BY Actor.lastname

不确定如何加入表格。

= = = = = = = = =

1 个答案:

答案 0 :(得分:2)

试试这个..

SELECT a.*
FROM Movie m
LEFT JOIN Cast c on (c.movieID = m.movieID)
LEFT JOIN Actor a on (a.actorID = c.actorID)
WHERE m.movieName='Apocalypse Now';

如果您还需要Studio加入工作室表:

SELECT a.actorID, a.firstName, a.lastName, s.studioName
FROM Movie m
LEFT JOIN Cast c on (c.movieID = m.movieID)
LEFT JOIN Actor a on (a.actorID = c.actorID)
LEFT JOIN Studio s on (m.studioID = s.studioID)
WHERE m.movieName='Apocalypse Now';