我有一个名为Home的视图:
CREATE VIEW `skohr`.`Home` AS
Select Song_OID, Song_Name, Album_Name, Artist_First_Name, Artist_Last_Name, Band_Name, Genre, Album_Art_File_Path
From Song, Album, Artist, Track_Number, Made
Where Song_OID = Track_Number.Song_OID_FK AND Album_OID=Track_Number.Album_OID_FK AND Album_OID=Made.Album_OID_FK AND Artist_OID=Made.Artist_OID_FK
ORDER BY Song_Name Asc
我在视图上运行了以下查询以获取我想要的表:
Select DISTINCT * From Home LEFT OUTER JOIN (SELECT * FROM Song_Setting, user WHERE User_OID=User_OID_FK) AS UserSongs
ON Song_OID=Song_OID_FK
如何将两个单独的查询合并为一个以创建单个视图?
答案 0 :(得分:1)
尝试创建视图
CREATE VIEW `skohr`.`MyNewView` AS
Select DISTINCT * From Home h
LEFT OUTER JOIN Song_Setting x
ON h.Song_OID_FK = x.Song_OID
INNER JOIN user u
ON u.User_OID = x.User_OID_FK
答案 1 :(得分:-1)
::对请求MySQL版本的评论的回复::
我今天没有专门使用MySQL,但是你有没有理由不像你的其他数据集一样使用子查询?像这样:
Select DISTINCT * From
(
Select Song_OID, Song_Name, Album_Name, Artist_First_Name, Artist_Last_Name, Band_Name, Genre, Album_Art_File_Path
From Song, Album, Artist, Track_Number, Made
Where Song_OID = Track_Number.Song_OID_FK AND Album_OID=Track_Number.Album_OID_FK AND Album_OID=Made.Album_OID_FK AND Artist_OID=Made.Artist_OID_FK
) as a
LEFT OUTER JOIN (SELECT * FROM Song_Setting, user WHERE User_OID=User_OID_FK) AS b
ON a.Song_OID=b.Song_OID_FK
如果这对MySQL不起作用,我想你可能会考虑使用临时/内存表。因此,请研究在MySQL中创建临时表,创建临时表并使用我在下面的CTE中捕获的子查询填充它,然后将其加入到您的其他查询中。
如果这是SQL Server,您可以使用CTEs
只需将以下代码放入View中即可获得所需内容。
WITH Home AS
(
Select Song_OID, Song_Name, Album_Name, Artist_First_Name, Artist_Last_Name, Band_Name, Genre, Album_Art_File_Path
From Song, Album, Artist, Track_Number, Made
Where Song_OID = Track_Number.Song_OID_FK AND Album_OID=Track_Number.Album_OID_FK AND Album_OID=Made.Album_OID_FK AND Artist_OID=Made.Artist_OID_FK
)
Select DISTINCT * From Home
LEFT OUTER JOIN (SELECT * FROM Song_Setting, user WHERE User_OID=User_OID_FK) AS UserSongs
ON Song_OID=Song_OID_FK