如何解决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,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,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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。