2012-07-23 60 views
1

我用5表我的照片库来存储不同的信息SQL查询来获取想要的结果从三个表

照片,PhotoDetails,PhotoMappings,天地我的相簿,PhotoAlbumCategory。

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

涉及的步骤。

  1. 我将照片添加到照片表
  2. 其次我映射的照片,以特别专辑(LanguageID,ALBUMID,AlbumCategoryID)在PhotoMapping表
  3. 第三,我需要添加与像标题特定的照片细节一起,描述,日期...

为了添加详细信息,我想显示特定相册下的用户列表以及附加信息,如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表

我想输出到像,如下所示假设我要列有关的详细信息,其中专辑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 

我希望关于我没有改变JOINLEFT OUTER JOIN & RIGHT OUTER JOIN,但放出来一样保持在此帮助

示例数据

映射表从PhotoDetails表

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 

示例数据

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 

回答

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 
+0

@KnowledgeSeeker请检查我的修改后的查询。 – 2012-07-23 09:56:57

+0

它显示了8行,来自AlbumID 10的其中一行和13以及在Details的所有字段中都带有“YES”。虽然它应该显示输出完全符合我在部分实际问题中显示的方式。 – Learning 2012-07-23 10:02:10

+0

我吓坏了这个查询数据似乎是正确的,我必须在某个地方做错什么...... – Learning 2012-07-23 10:03:55

0

左外连接PhotoMappings应该是工作之前:

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 
+0

我以前试过类似的查询,但结果保持不变。我试过你的查询,它也只是显示了第6行'期望的输出',因为我在实际问题的一部分中显示 – Learning 2012-07-23 09:48:14