-1

我在 SQL Server 2008 数据库中有 4 个具有以下架构的表

Tutorials (TutorialID, Title, Approved, AddedDate)
Albums    (AlbumID, Title, Approved, AddedDate)
Profiles  (ProfileID, Title, Approved, AddedDate)
Polls     (PollID, Title, Approved, AddedDate)

我想提供搜索工具来搜索所有这些表,并生成一个可分页的结果集,显示所有表的混合数据。

4

1 回答 1

1

您可能想要一个联合视图:

CREATE VIEW MixedData(Type, ID, Title, Approved, AddedDate) AS
    SELECT 'Tutorial' AS Type, TutorialID AS ID, Title, Approved, AddedDate
    UNION ALL
    SELECT 'Album'    AS Type, AlbumID    AS ID, Title, Approved, AddedDate
    UNION ALL
    SELECT 'Profile'  AS Type, ProfileID  AS ID, Title, Approved, AddedDate
    UNION ALL
    SELECT 'Poll'     AS Type, PollID     AS ID, Title, Approved, AddedDate

然后,您可以从MixedData应用您想要的标准中进行选择。“AS Type”和“AS ID”子句不是绝对必要的。

于 2012-12-08T19:52:23.787 回答