SQL Query从三个表中获取所需的结果

时间:2012-07-23 09:29:32

标签: sql sql-server-2008 tsql join

我在照片库中使用5张表来存储不同的信息

照片,PhotoDetails,PhotoMappings,PhotoAlbum,PhotoAlbumCategory。

这是一个多语种网站&我想上传相册的照片一次&与任何语言版本的网站分享。

涉及的步骤。

  1. 我将照片添加到照片表
  2. 第二我将照片与PhotoMapping表中的(LanguageID,AlbumID,AlbumCategoryID)一起映射到特定相册
  3. 第三,我需要添加与特定照片相关的详细信息,如标题,描述,日期......
  4. 为了添加详细信息,我想在特定相册下显示用户的照片列表以及其他信息,例如YES / NO,具体取决于此照片的详细信息是否在Photodetails表中。

    PhotoDetails
        [PhotoDetailsID] [int] IDENTITY(1,1) NOT NULL,
        [PhotoTitle] [nvarchar](300) NULL,
        [PhotoDesc] [nvarchar](max) NULL,
        [PhotoDate] [date] NULL,
        [PhotoVisible] [bit] NULL,
        [PhotoID] [int] NOT NULL,
        [AlbumID] [int] NULL,
        [LanguageID] [int] NULL,
        [PhotoDetailsCreatedOn] [date] NULL
    
    PhotoMappings
        [MappingID] [int] IDENTITY(1,1) NOT NULL,
        [LanguageID] [int] NULL,
        [CategoryID] [int] NULL,
        [AlbumID] [int] NULL,
        [PhotoID] [int] NULL,
    
    Photos
        [PhotoID] [int] IDENTITY(1,1) NOT NULL,
        [PhotoTN] [nvarchar](100) NULL,
        [PhotoLarge] [nvarchar](100) NULL,
        [PhotoGUID] [nvarchar](50) NULL,
        [PhotoCreatedOn] [date] NULL,
    
    
    SELECT d.PhotoTitle,d.AlbumID,p.PhotoTN AS TN,  p.PhotoID AS PID,p.PhotoLarge AS PL,
    case when d.PhotoId is null then 'NO' else 'YES' end AS [Details]
    FROM Photos p  JOIN  PhotoDetails d
    ON  p.PhotoId = d.PhotoId JOIN PhotoMappings m
    ON  p.PhotoId = m.PhotoId WHERE d.AlbumID  = 14 and m.LanguageID = 1
    

    我需要编写一个查询,它会显示PhotoMapping表中的所有记录以及PhotoDetails表中不存在的那些行。

    我使用上面的查询它得到了我想要的结果,但没有向我显示PhotoDetails表中不存在的列

    我希望OUTPUT如下所示,假设我想要有关AlbumID = 14&的专辑的详细信息。语言= 1(我的查询只得到前6行)

    PhotoTitle      AlbumID TN      PID PL  Details         
    Title of Photo1 14      1Icon.JPG   16  1.JPG   YES
    Title of Photo2 14      2Icon.JPG   21  2.JPG   YES
    Title of Photo3 14      3Icon.JPG   20  3.JPG   YES
    Title of Photo4 14      4Icon.JPG   22  4.JPG   YES
    Title of Photo5 14      5Icon.JPG   18  5.JPG   YES
    Title of Photo6 14      6Icon.JPG   17  6.JPG   YES
    Title of Photo7 14      7Icon.JPG   23  7.JPG   NO
    Title of Photo8 14      8Icon.JPG   24  8.JPG   NO
    

    我很感激帮助,因为我确实将JOIN更改为LEFT OUTER JOIN& RIGHT OUTER JOIN但出局保持不变

    样本数据

    映射表

    MappingID   LanguageID  CategoryID  AlbumID PhotoID
    1   1   7   14  16
    2   1   7   14  21
    3   1   7   14  20
    4   1   7   14  22
    5   1   7   14  19
    6   1   7   14  18
    7   1   7   14  17
    8   1   7   14  23
    

    PhotoDetails表中的示例数据

    PhotoDetailsID  PhotoTitle  PhotoDate   PhotoVisible    PhotoID AlbumID LanguageID  PhotoDetailsCreatedOn
    20  Title of Photo1     2012-07-02  1   16  14  1   2012-02-07
    21  Title of Photo2     2012-07-02  1   17  14  1   2012-02-07
    22  Title of Photo3     2012-07-02  1   18  14  1   2012-02-07
    24  Title of Photo4     2012-07-02  1   20  14  1   2012-02-07
    25  Title of Photo5     2012-07-02  1   21  14  1   2012-02-07
    26  Title of Photo6     2012-07-02  1   22  14  1   2012-02-07
    23  Title of Photo7     2012-07-02  1   19  10  1   2012-02-07
    27  Title of Photo8     2012-07-02  1   23  13  1   2012-02-07
    34  Something       2012-07-02  1   14  13  1   2012-02-07
    35  Something       2012-03-20  1   37  17  1   2012-03-07
    36  Something       2012-03-13  1   38  10  1   2012-03-07
    

2 个答案:

答案 0 :(得分:1)

在我看来,你需要改变两件事:AlbumID上的过滤器应该来自PhotoMappings,而不是PhotoDetail,你需要在PhotoDetails上进行左连接。

SELECT d.PhotoTitle,
       m.AlbumID,
       p.PhotoTN AS TN,
       p.PhotoID AS PID,
       p.PhotoLarge AS PL,
       case when d.PhotoId is null 
            then 'NO' 
            else 'YES' 
        end AS [Details]
  FROM Photos p
 INNER JOIN PhotoMappings m
    ON p.PhotoId = m.PhotoId
  LEFT JOIN  PhotoDetails d
    ON p.PhotoId = d.PhotoId
 WHERE m.AlbumID = 14
   and m.LanguageID = 1

答案 1 :(得分:0)

在PhotoMappings应该工作之前离开外部JOIN:

SELECT d.PhotoTitle,d.AlbumID,p.PhotoTN AS TN,  p.PhotoID AS PID,
p.PhotoLarge AS PL,
case when d.PhotoId is null then 'NO' else 'YES' end AS [Details]
FROM Photos p  JOIN  PhotoDetails d
ON  p.PhotoId = d.PhotoId 
left outer JOIN PhotoMappings m
ON  p.PhotoId = m.PhotoId 
WHERE d.AlbumID  = 14 and m.LanguageID = 1