SQL Server中两个SQL查询的联合

时间:2012-03-31 11:28:46

标签: sql-server-2008

我有一个问题是将SQL查询合并为一个,问题是我想要FirstName, LastName两次。首先是tblUploadedImages中的名称,然后是tblImageParticipants

中的名称
ALTER PROCEDURE GetFeaturedImages
AS
BEGIN
   SELECT 
       tblUser.FirstName AS FirstNameUploader, 
       tblUser.LastName AS LastNameUploader, 
       tblUser.UserID AS UserIDUploader 
   FROM 
       tblUploadedImages 
   INNER JOIN 
       tblUser ON tblUploadedImages.UserID = tblUser.UserID

   SELECT        
        tblUploadedImages.ImgUrl, 
        tblUploadedImages.ImgUrlOriginal, 
        tblUploadedImages.Description, 
        tblUploadedImages.ImageID, 
        tblUser.FirstName, 
        tblUser.LastName, 
        tblUploadedImages.UserID AS Uploader, 
        tblImageParticipants.UserID AS Participants
   FROM            
        tblImageParticipants  
   INNER JOIN
        tblUploadedImages ON tblImageParticipants.ImageID = tblUploadedImages.ImageID
   INNER JOIN
        tblUser ON tblImageParticipants.UserID = tblUser.UserID
   ORDER BY 
        tblUploadedImages.Added
END

我得到正确的结果只是我想在一个SQL查询中。我怎么能这样做?

3 个答案:

答案 0 :(得分:1)

您可以将两个查询与UNION结合使用,但列必须匹配(列数及其类型)。

因此,您的查询必须如下所示:

SELECT 
   tblUser.FirstName AS FirstNameUploader, 
   tblUser.LastName AS LastNameUploader, 
   tblUser.UserID AS UserIDUploader 
FROM 
   tblUploadedImages 
INNER JOIN 
   tblUser ON tblUploadedImages.UserID = tblUser.UserID

UNION

SELECT        
    tblUser.FirstName, 
    tblUser.LastName, 
    tblUploadedImages.UserID AS Uploader
FROM            
    tblImageParticipants  
INNER JOIN
    tblUploadedImages ON tblImageParticipants.ImageID = tblUploadedImages.ImageID
INNER JOIN
    tblUser ON tblImageParticipants.UserID = tblUser.UserID
ORDER BY 
    tblUploadedImages.Added

我省略了第二个查询中的其他列,以便两个查询都具有相同的列数。

如果您还需要第二个查询中的其他列,您可以保留第二个查询,但在第一个查询中添加“虚拟”列:

SELECT 
   '' as ImgUrl,
   '' as ImgUrlOriginal,
   '' as Description,
   0 as ImageID,
   tblUser.FirstName AS FirstNameUploader, 
   tblUser.LastName AS LastNameUploader, 
   tblUser.UserID AS UserIDUploader,
   0 as Participants
FROM 
   tblUploadedImages 
INNER JOIN 
   tblUser ON tblUploadedImages.UserID = tblUser.UserID

(SQL Server使用第一个查询中的列名,因此使用别名)

答案 1 :(得分:1)

我认为您正在寻找UNION ALLUNION删除重复项。我认为你想要的不是删除重复项。

SELECT 
   tblUser.FirstName AS FirstNameUploader, 
   tblUser.LastName AS LastNameUploader, 
   tblUser.UserID AS UserIDUploader 
FROM 
   tblUploadedImages 
INNER JOIN 
   tblUser ON tblUploadedImages.UserID = tblUser.UserID

UNION ALL

SELECT        
    tblUser.FirstName, 
    tblUser.LastName, 
    tblUploadedImages.UserID AS Uploader
FROM            
    tblImageParticipants  
INNER JOIN
    tblUploadedImages ON tblImageParticipants.ImageID = tblUploadedImages.ImageID
INNER JOIN
    tblUser ON tblImageParticipants.UserID = tblUser.UserID
ORDER BY 
    tblUploadedImages.Added

它也可能是godidé,因此您知道哪些是哪个。您可以为此添加其他列:

SELECT 
   tblUser.FirstName AS FirstNameUploader, 
   tblUser.LastName AS LastNameUploader, 
   tblUser.UserID AS UserIDUploader,
   'UploadedImages' AS Type
FROM 
   tblUploadedImages 
INNER JOIN 
   tblUser ON tblUploadedImages.UserID = tblUser.UserID

UNION ALL

SELECT        
    tblUser.FirstName, 
    tblUser.LastName, 
    tblUploadedImages.UserID AS Uploader,
    'ImageParticipants' AS Type
FROM            
    tblImageParticipants  
INNER JOIN
    tblUploadedImages ON tblImageParticipants.ImageID = tblUploadedImages.ImageID
INNER JOIN
    tblUser ON tblImageParticipants.UserID = tblUser.UserID
ORDER BY 
    tblUploadedImages.Added

我希望这会有所帮助

答案 2 :(得分:0)

您可以在第二个查询中的LEFT OUTER JOIN表上执行tblUploadedImages,如下所示:

SELECT        
        tblUploadedImages.ImgUrl, 
        tblUploadedImages.ImgUrlOriginal, 
        tblUploadedImages.Description, 
        tblUploadedImages.ImageID, 
        tblUser.FirstName, 
        tblUser.LastName, 
        tblUploadedImages.UserID AS Uploader, 
        tblImageParticipants.UserID AS Participants,
        u.FirstName AS FirstNameUploader,
        u.LastName AS LastNameUploader
   FROM            
        tblImageParticipants  
   INNER JOIN
        tblUploadedImages ON tblImageParticipants.ImageID = tblUploadedImages.ImageID
   INNER JOIN
        tblUser ON tblImageParticipants.UserID = tblUser.UserID
   LEFT OUTER JOIN
        tblUploadedImages i ON i.UserID = tblUser.UserID
   LEFT OUTER JOIN
        tblUser u ON u.UserID = i.UserID
   ORDER BY 
        tblUploadedImages.Added

您可能需要为其他表分配别名以避免歧义,但您可以先试用它。