开发者

Problem with Inner join in Sql-server 2005

开发者 https://www.devze.com 2023-03-12 22:43 出处:网络
Select FileUpload.FileName AS FINAME, F开发者_高级运维ileUpload.FilePath,MemberPersonalInformation.FirstName As SharedByfrom FileUpload
   Select FileUpload.FileName AS FINAME, F开发者_高级运维ileUpload.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)

While I execute this query getting an error in the last JOIN saying The multi-part identifier "ContentToIndividual.SubjectId" could not be bound. But I do have SubjectId in both the tables.I couldn't understand whats the problem.Please help me out.


You are joining SubjectMaster table to the ContentToIndividual table which you have not previously referenced.

You need to Join on to contentToIndvidual before referencing it in your SubjectMaster Join.

e.g.

 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)

Note: Even though you are querying ContentToIndividual in a sub query you can't reference the columns in that if it is not part of the main select query.


You have not joined ContentToIndividual in your main select statement. You need to add it in or not reference it.

EDIT: Just to add, you actually don't need to add the SubjectMaster or the ContentToIndividual joins in the main select, since you aren't selecting any columns from either table - remember that the sub query is separate from the main query; you're only using it to get the list of FileIds. It may also be possible to optimise the rest of the statement too.

   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)

EDIT 2: Just for fun, I think this might simplify things a little as it gets rid of the subqueries, so it should be faster...

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 
0

精彩评论

暂无评论...
验证码 换一张
取 消