Sql-server 2005中的内部联接问题

|
   Select FileUpload.FileName AS FINAME, FileUpload.FilePath,MemberPersonalInformation.FirstName As SharedBy  from FileUpload 
   INNER JOIN 
   ContentManagement ON ContentManagement.FileId=FileUpload.FileId  
   INNER JOIN 
   MemberPersonalInformation ON MemberPersonalInformation.MemberId=ContentManagement.CreatedBy 
   INNER JOIN
    SubjectMaster ON ContentToIndividual.SubjectId=SubjectMaster.SubjectId 
    where 
   FileUpload.FileId in
    (Select FileId from ContentManagement where ContentId in
    (Select ContentId from ContentToIndividual where ShowToMemberId=12) 
    AND ContentManagement.ContentTypeId=1 and ContentManagement.SessionId=4)
当我执行此查询时,在最后一个JOIN中显示错误消息“ 1”,但是两个表中确实都有SubjectId。我不明白是什么问题。请帮帮我。     
已邀请:
        您正在将
SubjectMaster
表连接到先前未引用的
ContentToIndividual
表。 您需要先加入
contentToIndvidual
才能在
SubjectMaster
Join中引用它。 例如
 Select FileUpload.FileName AS FINAME, 
        FileUpload.FilePath,MemberPersonalInformation.FirstName As SharedBy  
from FileUpload 
   INNER JOIN 
   ContentManagement ON ContentManagement.FileId=FileUpload.FileId  
   INNER JOIN 
   MemberPersonalInformation ON MemberPersonalInformation.MemberId=ContentManagement.CreatedBy 
    -- You need to add it in here   
    Inner Join ContentToIndividual on SomeColumn = AnotherColumn
   INNER JOIN
    SubjectMaster ON ContentToIndividual.SubjectId=SubjectMaster.SubjectId 
    where 
   FileUpload.FileId in
    (Select FileId from ContentManagement where ContentId in
    (Select ContentId from ContentToIndividual where ShowToMemberId=12) 
    AND ContentManagement.ContentTypeId=1 and ContentManagement.SessionId=4)
注意:即使您要在子查询中查询“ 3”,但如果它不是主选择查询的一部分,也不能引用其中的列。     
        您尚未在主要的select语句中加入ContentToIndividual。您需要添加它或不引用它。 编辑:只是要添加,实际上您不需要在主选择中添加
SubjectMaster
ContentToIndividual
联接,因为您没有从任何一个表中选择任何列-请记住,子查询与主查询是分开的;您仅使用它来获取FileId列表。也有可能优化语句的其余部分。
   Select FileUpload.FileName AS FINAME, FileUpload.FilePath,MemberPersonalInformation.FirstName As SharedBy  from FileUpload 
   INNER JOIN 
   ContentManagement ON ContentManagement.FileId=FileUpload.FileId  
   INNER JOIN 
   MemberPersonalInformation ON MemberPersonalInformation.MemberId=ContentManagement.CreatedBy 
   where 
   FileUpload.FileId in
    (Select FileId from ContentManagement where ContentId in
    (Select ContentId from ContentToIndividual where ShowToMemberId=12) 
    AND ContentManagement.ContentTypeId=1 and ContentManagement.SessionId=4)
编辑2:只是为了好玩,我认为这可以简化一些事情,因为它摆脱了子查询,因此应该更快。
SELECT      FileUpload.FileName AS FINAME, FileUpload.FilePath,MemberPersonalInformation.FirstName As SharedBy
FROM        FileUpload 
INNER JOIN  ContentManagement ON ContentManagement.FileId=FileUpload.FileId
            AND ContentManagement.ContentTypeId=1
            AND ContentManagement.SessionId=4
INNER JOIN  ContentToIndividual ON ContentToIndividual.ContentId = ContentManagement.ContentId -- Iguessed at this join
            AND ContentToIndividual.ShowToMemberId=12
INNER JOIN  MemberPersonalInformation ON MemberPersonalInformation.MemberId = ContentManagement.CreatedBy 
    

要回复问题请先登录注册