我从此查询中获取了许多重复的行。我的左连接有什么问题吗?我试过内线加入,但他们根本没有帮助。我认为问题是如果一个左连接是真的那么它会在结果中添加一行
SELECT DP.FirstName, DP.LastName, DECA.Email, UI.Id, ET.EmplId,
HPPTR.OrganizationUnitId, A.CountryRegionId, ET.EmplIdentNumber,
ET.JobId_INN, HPPTR.Description, ET.HRMResponsible_INN, A.Street, A.ZipCode,
A.City, HPER.ValidFromDateTime, HCJF.INN_JobAgreementType,
HPJTR.INN_EmpJobType, HPPTR.hrmemploymentfactor
FROM DirPartyTable AS DP
LEFT JOIN DIRPARTYECOMMUNICATIONRELA2608 AS DPECR ON DP.PartyId = DPECR.PartyId
LEFT JOIN DirECommunicationAddress AS DECA ON DPECR.ValuesRecId = DECA.RecId
LEFT JOIN EmplTable AS ET ON DP.PartyId = ET.PartyId
LEFT JOIN SYSCOMPANYUSERINFO AS SCUI ON ET.EmplId = SCUI.EmplId
LEFT JOIN USERINFO AS UI ON SCUI.UserId = UI.Id
LEFT JOIN HRPPARTYPOSITIONTABLERELAT2226 AS HPPTR ON ET.EmplId = HPPTR.Reference
LEFT JOIN HRPPartyJobTableRelationship AS HPJTR ON HPPTR.JobId = HPJTR.Jobid
LEFT JOIN HRMCompJobFunction AS HCJF ON HPJTR.HrmCompJobFunctionId = HCJF.JobFunctionId
LEFT JOIN Address AS A ON ET.RecId = A.AddrRecId
LEFT JOIN HRMPARTYEMPLOYEERELATIONSHIP AS HPER ON ET.EmplId = HPER.EmplId
WHERE DP.DataAreaId != 'cvc'
AND DECA.ECommunicationTypeId = 'email'
AND A.addrtableid='256'
AND UI.NETWORKALIAS IS NOT NULL
AND A.DataAreaId = DP.DataAreaId
AND SCUI.DataAreaId = DP.DataAreaId
AND ET.DataAreaId = DP.DataAreaId
ORDER BY ET.EmplId
这是架构
FirstName LastName Email Id EmplId OrganizationUnitId CountryRegionId EmplIdentNumber CountryRegionId Description Street ZipCode City ValidFromDateTime INN_JobAgreementType INN_EmpJobType hrmemploymentfactor
John Head john.head@example.com johnh headjh org1 FI 131564-234523 CHZ IT GUY rockstreet 534523 Bigplace 2009-02-16 00:00:00.000 NULL 1.000000000000
John Head john.head@example.com johnh headjh org2 FI 131564-234524 CHZ IT GUY rockstreet 534523 Bigplace 2009-02-16 00:00:00.000 NULL 1.000000000000
John Head john.head@example.com johnh headjh org3 FI 131564-234525 CHZ IT GUY rockstreet 534523 Bigplace 2009-02-16 00:00:00.000 NULL 1.000000000000
John Head john.head@example.com johnh headjh org4 FI 131564-234526 CHZ IT GUY rockstreet 534523 Bigplace 2009-02-16 00:00:00.000 NULL 1.000000000000
John Head john.head@example.com johnh headjh org5 FI 131564-234527 CHZ IT GUY rockstreet 534523 Bigplace 2009-02-16 00:00:00.000 NULL 0.000000000000
John Head john.head@example.com johnh headjh org1 FI 131564-234528 CHZ IT GUY rockstreet 534523 Bigplace 2009-02-16 00:00:00.000 NULL 1.000000000000
John Head john.head@example.com johnh headjh org2 FI 131564-234529 CHZ IT GUY rockstreet 534523 Bigplace 2009-02-16 00:00:00.000 NULL 1.000000000000
John Head john.head@example.com johnh headjh org3 FI 131564-234530 CHZ IT GUY rockstreet 534523 Bigplace 2009-02-16 00:00:00.000 NULL 1.000000000000
John Head john.head@example.com johnh headjh org4 FI 131564-234531 CHZ IT GUY rockstreet 534523 Bigplace 2009-02-16 00:00:00.000 NULL 1.000000000000
John Head john.head@example.com johnh headjh org5 FI 131564-234532 CHZ IT GUY rockstreet 534523 Bigplace 2009-02-16 00:00:00.000 NULL 0.000000000000
John Head john.head@example.com johnh headjh org1 FI 131564-234533 CHZ IT GUY rockstreet 534523 Bigplace 2009-01-19 00:00:00.000 NULL 1.000000000000
John Head john.head@example.com johnh headjh org2 FI 131564-234534 CHZ IT GUY rockstreet 534523 Bigplace 2009-01-19 00:00:00.000 NULL 1.000000000000
John Head john.head@example.com johnh headjh org3 FI 131564-234535 CHZ IT GUY rockstreet 534523 Bigplace 2009-01-19 00:00:00.000 NULL 1.000000000000
John Head john.head@example.com johnh headjh org4 FI 131564-234536 CHZ IT GUY rockstreet 534523 Bigplace 2009-01-19 00:00:00.000 NULL 1.000000000000
John Head john.head@example.com johnh headjh org5 FI 131564-234537 CHZ IT GUY rockstreet 534523 Bigplace 2009-01-19 00:00:00.000 NULL 0.000000000000
John Head john.head@example.com johnh headjh org1 FI 131564-234538 CHZ IT GUY rockstreet 534523 Bigplace 2009-01-19 00:00:00.000 NULL 1.000000000000
John Head john.head@example.com johnh headjh org2 FI 131564-234539 CHZ IT GUY rockstreet 534523 Bigplace 2009-01-19 00:00:00.000 NULL 1.000000000000
John Head john.head@example.com johnh headjh org3 FI 131564-234540 CHZ IT GUY rockstreet 534523 Bigplace 2009-01-19 00:00:00.000 NULL 1.000000000000
John Head john.head@example.com johnh headjh org4 FI 131564-234541 CHZ IT GUY rockstreet 534523 Bigplace 2009-01-19 00:00:00.000 NULL 1.000000000000
John Head john.head@example.com johnh headjh org5 FI 131564-234542 CHZ IT GUY rockstreet 534523 Bigplace 2009-01-19 00:00:00.000 NULL 0.000000000000
John Head john.head@example.com johnh headjh org1 FI 131564-234543 CHZ IT GUY rockstreet 534523 Bigplace 2011-01-01 00:00:00.000 NULL 1.000000000000
John Head john.head@example.com johnh headjh org2 FI 131564-234544 CHZ IT GUY rockstreet 534523 Bigplace 2011-01-01 00:00:00.000 NULL 1.000000000000
John Head john.head@example.com johnh headjh org3 FI 131564-234545 CHZ IT GUY rockstreet 534523 Bigplace 2011-01-01 00:00:00.000 NULL 1.000000000000
John Head john.head@example.com johnh headjh org4 FI 131564-234546 CHZ IT GUY rockstreet 534523 Bigplace 2011-01-01 00:00:00.000 NULL 1.000000000000
John Head john.head@example.com johnh headjh org5 FI 131564-234547 CHZ IT GUY rockstreet 534523 Bigplace 2011-01-01 00:00:00.000 NULL 0.000000000000
John Head john.head@example.com johnh headjh org1 FI 131564-234548 CHZ IT GUY rockstreet 534523 Bigplace 2011-01-01 00:00:00.000 NULL 1.000000000000
John Head john.head@example.com johnh headjh org2 FI 131564-234549 CHZ IT GUY rockstreet 534523 Bigplace 2011-01-01 00:00:00.000 NULL 1.000000000000
John Head john.head@example.com johnh headjh org3 FI 131564-234550 CHZ IT GUY rockstreet 534523 Bigplace 2011-01-01 00:00:00.000 NULL 1.000000000000
John Head john.head@example.com johnh headjh org4 FI 131564-234551 CHZ IT GUY rockstreet 534523 Bigplace 2011-01-01 00:00:00.000 NULL 1.000000000000
John Head john.head@example.com johnh headjh org5 FI 131564-234552 CHZ IT GUY rockstreet 534523 Bigplace 2011-01-01 00:00:00.000 NULL 0.000000000000
John Head john.head@example.com johnh headjh org1 FI 131564-234553 CHZ IT GUY rockstreet 534523 Bigplace 2011-01-02 00:00:00.000 NULL 1.000000000000
John Head john.head@example.com johnh headjh org2 FI 131564-234554 CHZ IT GUY rockstreet 534523 Bigplace 2011-01-02 00:00:00.000 NULL 1.000000000000
John Head john.head@example.com johnh headjh org3 FI 131564-234555 CHZ IT GUY rockstreet 534523 Bigplace 2011-01-02 00:00:00.000 NULL 1.000000000000
John Head john.head@example.com johnh headjh org4 FI 131564-234556 CHZ IT GUY rockstreet 534523 Bigplace 2011-01-02 00:00:00.000 NULL 1.000000000000
John Head john.head@example.com johnh headjh org5 FI 131564-234557 CHZ IT GUY rockstreet 534523 Bigplace 2011-01-02 00:00:00.000 NULL 0.000000000000
John Head john.head@example.com johnh headjh org1 FI 131564-234558 CHZ IT GUY rockstreet 534523 Bigplace 2011-01-02 00:00:00.000 NULL 1.000000000000
John Head john.head@example.com johnh headjh org2 FI 131564-234559 CHZ IT GUY rockstreet 534523 Bigplace 2011-01-02 00:00:00.000 NULL 1.000000000000
John Head john.head@example.com johnh headjh org3 FI 131564-234560 CHZ IT GUY rockstreet 534523 Bigplace 2011-01-02 00:00:00.000 NULL 1.000000000000
John Head john.head@example.com johnh headjh org4 FI 131564-234561 CHZ IT GUY rockstreet 534523 Bigplace 2011-01-02 00:00:00.000 NULL 1.000000000000
John Head john.head@example.com johnh headjh org5 FI 131564-234562 CHZ IT GUY rockstreet 534523 Bigplace 2011-01-02 00:00:00.000 NULL 0.000000000000
John Head john.head@example.com johnh headjh org1 FI 131564-234563 CHZ IT GUY rockstreet 534523 Bigplace 2010-02-04 00:00:00.000 NULL 1.000000000000
John Head john.head@example.com johnh headjh org2 FI 131564-234564 CHZ IT GUY rockstreet 534523 Bigplace 2010-02-04 00:00:00.000 NULL 1.000000000000
John Head john.head@example.com johnh headjh org3 FI 131564-234565 CHZ IT GUY rockstreet 534523 Bigplace 2010-02-04 00:00:00.000 NULL 1.000000000000
John Head john.head@example.com johnh headjh org4 FI 131564-234566 CHZ IT GUY rockstreet 534523 Bigplace 2010-02-04 00:00:00.000 NULL 1.000000000000
John Head john.head@example.com johnh headjh org5 FI 131564-234567 CHZ IT GUY rockstreet 534523 Bigplace 2010-02-04 00:00:00.000 NULL 0.000000000000
John Head john.head@example.com johnh headjh org1 FI 131564-234568 CHZ IT GUY rockstreet 534523 Bigplace 2010-02-04 00:00:00.000 NULL 1.000000000000
John Head john.head@example.com johnh headjh org2 FI 131564-234569 CHZ IT GUY rockstreet 534523 Bigplace 2010-02-04 00:00:00.000 NULL 1.000000000000
John Head john.head@example.com johnh headjh org3 FI 131564-234570 CHZ IT GUY rockstreet 534523 Bigplace 2010-02-04 00:00:00.000 NULL 1.000000000000
John Head john.head@example.com johnh headjh org4 FI 131564-234571 CHZ IT GUY rockstreet 534523 Bigplace 2010-02-04 00:00:00.000 NULL 1.000000000000
John Head john.head@example.com johnh headjh org5 FI 131564-234572 CHZ IT GUY rockstreet 534523 Bigplace 2010-02-04 00:00:00.000 NULL 0.000000000000
答案 0 :(得分:3)
您需要考虑“正确”栏目是否唯一。
例如:在
FROM DirPartyTable AS DP
LEFT JOIN DIRPARTYECOMMUNICATIONRELA2608 AS DPECR ON DP.PartyId = DPECR.PartyId
DPECR.PartyID
是唯一的吗?如果不是,那么DP中映射到DPECR中多行的每一行都会出现多次。
...这会继续进行所有其他联接。语法方面,它们很好,但你需要问自己独特性。
要弄清楚发生了什么,请尝试执行SELECT 1
而不是字段列表,并从第一个表开始,查看返回的行数。然后尝试前两个或三个表,直到你可以找出哪些连接使得返回的行数增加。那些是你的问题,需要进一步调查你的桌子设计。