SQL连接多个表

时间:2011-12-15 15:36:20

标签: sql sql-server join

我正在尝试编写SQL查询,该查询将返回给定存款的房东信息,房产信息和主要租户信息。但是,我的查询似乎返回一行,可能是与该存款相关的每个租户而不是主租户,我想知道我哪里出错了。

SQL代码如下。

SELECT
IIF.ImportFileid,
Deposit.CertificateNumber AS DPCNumber,
Member.FirstNames AS LandlordFirstName, Member.LastNames AS LandlordLastNames, 
Propertys.FullAddress, 
Tenant.FirstNames AS LeadTenantFirstName, Tenant.LastNames AS LeadTenantLastNames

FROM tblMemberPropertyDepositTenantHistoryNominatedHistory 
INNER JOIN tblMemberPropertyDepositTenantHistory AS Tenant
ON tblMemberPropertyDepositTenantHistoryNominatedHistory.MemberPropertyDepositTenantHistoryID = Tenant.MemberPropertyDepositTenantHistoryID

INNER JOIN tblMemberPropertyDeposits AS Deposit
ON Tenant.MemberPropertyDepositID = Deposit.MemberPropertyDepositID

INNER JOIN tblMemberProperties AS Propertys
ON Deposit.MemberPropertyID = Propertys.MemberPropertyID

INNER JOIN tblMembers AS Member
ON Propertys.MemberID = Member.MemberID

INNER JOIN import.CommonImportDeposits AS CID
ON Member.MemberID = CID.MemberID

INNER JOIN import.ImportFiles AS IIF
ON CID.ImportFileID = IIF.ImportFileID

WHERE IIF.ImportFileID = 1091

希望您可以从表的命名约定中看出它们之间的关系。

1 个答案:

答案 0 :(得分:1)

任何编写存储过程的人都在创建重复的指定租户。 对不起伙计们。