select Txn.txnRecNo
from Txn
inner join Person on Txn.uwId = Person.personId
full outer join TxnInsured on Txn.txnRecNo = TxnInsured.txnRecNo
left join TxnAdditionalInsured on Txn.txnRecNo = TxnAdditionalInsured.txnRecNo
where Txn.visibleFlag=1
and Txn.workingCopy=1
返回了20条记录
select Txn.txnRecNo
from Txn
inner join Person on Txn.uwId = Person.personId
full outer join TxnInsured on Txn.txnRecNo = TxnInsured.txnRecNo
where Txn.visibleFlag=1
and Txn.workingCopy=1
返回了15条记录
答案 0 :(得分:2)
我怀疑TxnAdditionalInsured
表有重复记录。使用distinct
select distinct Txn.txnRecNo
from Txn
inner join Person on Txn.uwId = Person.personId
full outer join TxnInsured on Txn.txnRecNo = TxnInsured.txnRecNo
left join TxnAdditionalInsured on Txn.txnRecNo = TxnAdditionalInsured.txnRecNo
where Txn.visibleFlag=1
and Txn.workingCopy=1
答案 1 :(得分:0)
试试这个:
select distinct Txn.txnRecNo --> added distinct here
from Txn
inner join Person on Txn.uwId = Person.personId
full outer join TxnInsured on Txn.txnRecNo = TxnInsured.txnRecNo
left join TxnAdditionalInsured on Txn.txnRecNo = TxnAdditionalInsured.txnRecNo
where Txn.visibleFlag=1
and Txn.workingCopy=1
答案 2 :(得分:0)
left
联接会在结果集中生成连接左侧的所有行,至少一次。
但是如果您的连接条件是右侧的多个行与左侧的特定行匹配,则该左侧行将在结果中多次出现(多次为它与右行相匹配。
因此,如果结果是意料之外的,那么您的加入条件并不严格,或者您不了解自己的数据。
与其他答案不同,我不建议只添加distinct
- 我建议您调查数据并确定您的ON
条款是否需要加强,或者您的数据是否实际上是错误的。添加distinct
以“使结果看起来正确”通常是一个糟糕的决定 - 更愿意调查并获取正确的查询。