1

我在下面有一个视图,其中包含我想要显示的所有内容,但是只有在每个表格中为特定电影添加所有内容时它才会显示(如果这样的话)。就像添加了电影一样,只有在副本、演员、角色和流派已与该特定电影相关联时才会显示视图,但是如果没有为电影添加任何内容,则不会显示。我是这个东西的新手,所以任何帮助将不胜感激。

CREATE VIEW viewAllMovieDetails AS
SELECT `tblMovie`.`MovieName`, `tblCopy`.`CpyEdition`, `tblCopy`.`CpyCondition`,
        `tblCopy`.`CpyAmountPaid`, `tblCopy`.`CpyNote`, `tblMovie`.`MovieRating`, 
        `tblMovie`.`MovieLength`,`tblMovie`.`MovieYear`, `tblActor`.`ActLastName`, 
        `tblActor`.`ActFirstName`, `tblRole`.`Role`, `tblMovieGenre`.`GenGenre`
FROM `tblCopy`, `tblMovie`, `tblActor`, `tblRole`, `tblMovieGenre`    
WHERE `tblCopy`.`MovieID` = `tblMovie`.`MovieID` AND `tblRole`.`MovieID` = `tblMovie`.`MovieID` 
    AND `tblRole`.`ActID` = `tblActor`.`ActID` AND `tblMovieGenre`.`MovieID` =                 `tblMovie`.`MovieID`;
4

2 回答 2

2

您应该使用左连接。如果它们是任何 NULL,它们将显示在这种类型的连接中。

CREATE VIEW viewAllMovieDetails AS
SELECT tblMovie.MovieName, tblCopy.CpyEdition, tblCopy.CpyCondition,
        tblCopy.CpyAmountPaid, tblCopy.CpyNote, tblMovie.MovieRating, 
        tblMovie.MovieLength,tblMovie.MovieYear, tblActor.ActLastName, 
        tblActor.ActFirstName, tblRole.Role, tblMovieGenre.GenGenre
FROM tblCopy   
LEFT JOIN tblMovie tm USING(MovieID)
LEFT JOIN tblActor ta USING(ActID)
LEFT JOIN tblRole tr ON tr.ActID = ta.ActID
LEFT JOIN tblMovieGenre tg ON tg.MovieID = tm.MovieID;

使用别名也是一种很好的做法;)如果您将条件放在获得内部而不是左连接的位置。

于 2013-06-11T22:37:00.023 回答
2

使用“左连接”而不是“位置”

CREATE VIEW viewAllMovieDetails AS
SELECT `tblMovie`.`MovieName`, `tblCopy`.`CpyEdition`, `tblCopy`.`CpyCondition`,
        `tblCopy`.`CpyAmountPaid`, `tblCopy`.`CpyNote`, `tblMovie`.`MovieRating`, 
        `tblMovie`.`MovieLength`,`tblMovie`.`MovieYear`, `tblActor`.`ActLastName`, 
        `tblActor`.`ActFirstName`, `tblRole`.`Role`, `tblMovieGenre`.`GenGenre`
FROM `tblMovie` left join tblCopy on tblMovie.mouvieId=tblCopy.mouvieId ...etc
于 2013-06-11T22:37:43.440 回答