我有 2 张桌子:
发票
CustomerInvoiceID - PK
RecordLocator - Text
createdDate - date
简化发票
CustomerInvoiceID - PK / FK Invoices
CountryCode - text
VatPercentage - number
SubtotalAmount - number
TotalAmount - number
如果我运行以下查询:
select
si.CountryCode,
si.RecordLocator,
si.CustomerInvoiceID,
i.CreatedDate,
i.VatPercentage,
si.SubTotalAmount,
si.TotalAmount
from
SimpliedInvoice si , invoice i
where
si.CustomerInvoiceID = i.CustomerInvoiceID
and si.CountryCode = 'GR'
我的结果是一个空数据集。
但如果我运行这个查询:
select *
from SimpliedInvoice
where CustomerInvoiceID in (select CustomerInvoiceID
from Invoice
where CountryCode = 'GR')
我得到了大量的数据。
我不明白为什么,有什么想法吗?
好吧,在您的第一个查询中,您检查
SimplifiedInvoice
表(别名si
)中的 CountryCode = 'GR' -但在第二个查询中,您正在子查询中测试同一国家/地区代码的发票表:
因此,将您的第一个查询更改为:
并且您应该得到返回的同一组数据
在第一个查询中,CountryCode 来自 SimpliedInvoice 表;在第二个查询中,CountryCode 来自 Invoice 表,因此它们没有查询相同的数据。这表明 SimpliedInvoice 中的数据可能与发票表不同步,假设目的是使它们保持同步。