繁体   English   中英

SQL查询以查找共享键的两个表之间的丢失的记录

[英]SQL Query to find missing records between two tables that share a key

就像标题要求的那样,我正在尝试寻找一种方法来给我一些交易差异的数字。 用简单的英语来说的问题是:数据库中有一些不良数据。 一些销售交易缺少有关哪种产品以及售出了多少的数据。

Salestransaction(表1)列:transactionid,customerid,storeid,tdate

Soldvia(表2)栏:transactionid,productid,noofitems

任何帮助,将不胜感激。 如果我不清楚,请告诉我,我将提供更多信息。 谢谢。

这是简单的LEFT JOIN其中过滤了null

对于Soldvia表中的缺失物品:

select count(*) as difference 
from Salestransaction st
left join Soldvia s on st.transactionid = s.transactionid 
where s.transactionid is null

对于两个表中的缺失项:

select count(*) as difference 
from Salestransaction st
full join Soldvia s on st.transactionid = s.transactionid 
where st.transactionid is null or s.transactionid is null

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM