1

我有两张桌子:

1) 博客相册:

Id  AlbumName   CoverPhoto          CreatedDate                    Description     
1   Onam        noImage.jpg     2012-07-05 10:54:46.977            Onam is an ancient festival which still survives in modern times. Kerala's rice harvest festival and the Festival of Rain Flowers, which fall on the Malayalam month of Chingam, celebrates the Asura King Mahabali's annual visit from Patala (the underworld). Onam is unique since Mahabali has been revered by the people of Kerala since prehistory.    According to the legend, Kerala witnessed its golden era during the reign of King Mahabali. The Brahma-Vaivarta Puranam explains that Lord Vishnu wante     
2   Birds       noImage.jpg     2012-07-05 11:02:48.667         
3   Nature    gold-fish.jpg     2012-07-05 11:03:36.503

2. 博客照片:

    Id  AlbumId PhotoName                  AddedDate            
    29  1   Photo0364.jpg      2012-07-05 11:01:18.270          
    30  1   Photo0380.jpg      2012-07-05 11:01:35.723         
    31  1   Photo0404.jpg      2012-07-05 11:01:47.717          
    32  1   Photo0404.jpg      2012-07-05 11:02:34.457          
    33  2   f.jpg              2012-07-05 11:03:03.300          
    34  2   g.jpg              2012-07-05 11:03:12.917         
    35  3   image005.jpg       2012-07-05 11:03:45.367          
    36  3   1.jpg              2012-07-05 11:03:57.837          
    37  3   2.jpg              2012-07-05 11:04:05.580          
    38  3   na.jpg             2012-07-05 11:04:17.337          
    39  3   gold-fish.jpg      2012-07-05 11:06:29.453

我写了一个存储过程来列出 BlogPhoto 的详细信息:

ALTER PROCEDURE [dbo].[BlogPhotoDetailsById]
    (
        @Id int
    )
    AS
    BEGIN
        SELECT [HRM_BlogPhoto].[Id] 
              ,[HRM_BlogPhoto].[AlbumId] 
              ,[HRM_BlogPhoto].[PhotoName] 
              ,[HRM_BlogPhoto].[AddedDate] 
              ,[HRM_BlogPhoto].[Title] 
              ,[HRM_BlogPhoto].[ModifiedDate]
          FROM [HRM_BlogPhoto] 
        WHERE [HRM_BlogPhoto].[Id] = @Id 

    END

我想要的是,如果 BlogPhoto.PhotoName 等于 BlogAlbum.CoverPhoto,那么在上面的存储过程中我需要显示“true”,如果不是,我需要显示“false”。

如何修改上述存储过程?

4

1 回答 1

1

下面将显示 BlogPhoto 是否是至少一个 BlogAlbum 的 CoverPhoto(注意一张照片可能在多个相册中)

ALTER PROCEDURE [dbo].[BlogPhotoDetailsById] (@Id int) 开始选择 [HRM_BlogPhoto].[Id] ,[HRM_BlogPhoto].[AlbumId] ,[HRM_BlogPhoto].[PhotoName] ,[HRM_BlogPhoto].[AddedDate] ,[ HRM_BlogPhoto].[Title] ,[HRM_BlogPhoto].[ModifiedDate] ,CASE WHEN EXISTS (SELECT * FROM [blogalbum] WHERE [blogalbum].CoverPhoto = [HRM_BlogPhoto].[PhotoName] ) THEN 'true' ELSE 'false' END AS [IsCoverPhoto] FROM [HRM_BlogPhoto] WHERE [HRM_BlogPhoto].[Id] = @Id

编辑

Bennor 的观点被采纳了。下面将避免嵌套循环,但请注意,由于可能出现重复的封面照片,您需要添加 distinct 关键字。

ALTER PROCEDURE [dbo].[BlogPhotoDetailsById]
(
    @Id int
)
AS
BEGIN
    SELECT DISTINCT 
          [HRM_BlogPhoto].[Id]
          ,[HRM_BlogPhoto].[AlbumId] 
          ,[HRM_BlogPhoto].[PhotoName] 
          ,[HRM_BlogPhoto].[AddedDate] 
          ,[HRM_BlogPhoto].[Title] 
          ,[HRM_BlogPhoto].[ModifiedDate]
          ,CASE WHEN [blogalbum].CoverPhoto IS NOT NULL
             THEN 'true' 
             ELSE 'false' 
           END AS [IsCoverPhoto]
      FROM [HRM_BlogPhoto] 
            LEFT OUTER JOIN [blogalbum]
            ON [blogalbum].CoverPhoto = [HRM_BlogPhoto].[PhotoName]
    WHERE [HRM_BlogPhoto].[Id] = @Id     

一个警告 - 出于性能原因,如果您采用这种方法,您应该确保在 [blogalbum].CoverPhoto 上有一个索引。

于 2012-07-06T05:30:51.553 回答