即使为NULL,也显示所有记录

时间:2013-06-11 22:27:52

标签: mysql database view

我在下面有一个视图,其中包含我要显示的所有内容,但是只有在每个表格中为特定电影添加所有内容时才会显示(如果这样做)。就像添加了电影一样,只有在复制,演员,角色和类型已连接到该特定电影时才会显示视图,但是如果没有为电影添加任何内容,则不会显示该视频。我是新手,所以任何帮助都将不胜感激。

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`;

2 个答案:

答案 0 :(得分:2)

你应该使用LEFT JOIN。如果它们是任何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;

使用别名也是很好的做法;) 如果你把条件放在内部而不是左连接的地方。

答案 1 :(得分:2)

使用“left join”而不是“where”

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