简体   繁体   中英

Problem with Inner join in 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)

While I execute this query getting an error in the last JOIN saying 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.

eg

 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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM