1

我有一个名为 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

如何将两个单独的查询合并为一个以创建单个视图?

4

2 回答 2

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  
于 2012-12-03T22:10:39.553 回答
-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,您可以使用CTE

只需将以下代码放入视图中,它就会为您提供所需的内容。

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
于 2012-12-03T22:15:28.477 回答