简体   繁体   English

如何从多个表中获取所有记录,甚至没有值的记录

[英]How to get all records from multiple tables, even those with no values

So I 've got many tables for a movies database (Films, FilmPersons, Persons, PersonEmails, Roles, FilmGenres, Genres). 因此,我有许多用于电影数据库的表(电影,FilmPersons,Person,PersonEmail,Roles,FilmGenres,Genres)。

I want to output all films that exist with as many information as possible. 我想输出存在的所有电影,并提供尽可能多的信息。 For example one row would be: 例如,一行将是:

Title | Date | Budget | Actor | Director | Genre |  
Alien | 1923 | 3322 | someActor | someDirector | Scifi
SELECT        Films.Title, Films.Date, Films.Budget, Persons.FirstName, Persons.LastName, Roles.RoleName, Genres.Name 
FROM            Films INNER JOIN
                     FilmPersons ON Films.FilmID = FilmPersons.FilmID INNER JOIN
                     Persons ON FilmPersons.PersonID = Persons.PersonID INNER JOIN
                     Roles ON FilmPersons.RoleID = Roles.RoleID INNER JOIN
                     FilmGenres ON Films.FilmID = FilmGenres.FilmID INNER JOIN
                     Genres ON FilmGenres.GenreID = Genres.GenreID

But the above result don't show all the information about the movie, how to change the SQL so that I can select all fields? 但是以上结果并未显示有关电影的所有信息,如何更改SQL以选择所有字段?

Basicly, I suppose you should use LEFT JOIN instead of INNER JOIN on such situation: 基本上,我想您应该在这种情况下使用LEFT JOIN而不是INNER JOIN

SELECT Films.Title, 
  Films.Date, 
  Films.Budget, 
  Persons.FirstName, 
  Persons.LastName, 
  Roles.RoleName, 
  Genres.Name 
FROM Films LEFT JOIN FilmPersons ON Films.FilmID = FilmPersons.FilmID 
INNER JOIN Persons ON FilmPersons.PersonID = Persons.PersonID 
INNER JOIN Roles ON FilmPersons.RoleID = Roles.RoleID 
LEFT JOIN FilmGenres ON Films.FilmID = FilmGenres.FilmID 
INNER JOIN Genres ON FilmGenres.GenreID = Genres.GenreID

While using LEFT JOIN, 在使用LEFT JOIN时,

FROM Films **LEFT JOIN** FilmPersons ON Films.FilmID = FilmPersons.FilmID

It would make sure that Films are the main collection, even some value of FilmPersons were missing. 这样就可以确保电影是主要收藏,甚至电影人的某些价值都将丢失。 If you are using INNER JOIN , the final result of the query would be Intersection . 如果使用INNER JOIN ,则查询的最终结果将是Intersection

I'm not sure my explain is understandable.. 我不确定我的解释是否可以理解。

Whats going to happen is for every row in films SQL Server will attempt to find matching rows in filepersons (if it does not find a matching row it will place nulls in the values that don't match) and so on. 对于电影中的每一行,将发生什么情况,SQL Server将尝试在文件人中查找匹配的行(如果找不到匹配的行,它将在不匹配的值中放置null),依此类推。 Where ever it finds a match it will put the values it finds in the matching columns otherwise it will place nulls in those columns. 无论在哪里找到匹配项,都会将找到的值放在匹配列中,否则会将空值放在这些列中。

SELECT Films.Title, 
  Films.Date, 
  Films.Budget, 
  Persons.FirstName, 
  Persons.LastName, 
  Roles.RoleName, 
  Genres.Name 
FROM Films LEFT JOIN FilmPersons ON Films.FilmID = FilmPersons.FilmID 
left JOIN Persons ON FilmPersons.PersonID = Persons.PersonID 
left JOIN Roles ON FilmPersons.RoleID = Roles.RoleID 
LEFT JOIN FilmGenres ON Films.FilmID = FilmGenres.FilmID 
left JOIN Genres ON FilmGenres.GenreID = Genres.GenreID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM