简体   繁体   English

查找另一个表中不存在的 ID

[英]Find ID which doesn't exist from another table

I am studying SQL and I am not sure which is the way to filtering data.我正在研究 SQL,我不确定过滤数据的方式是什么。

For example, there I have two tables:例如,我有两个表:

Reference_OrderTable : Reference_OrderTable

  • OrderID订单编号
  • Item物品
  • Price价格

OrderTable : OrderTable

  • OrderID订单编号
  • Item物品
  • Price价格
  • Reference_Ordertable : this table has all the type of orders. Reference_Ordertable :该表包含所有订单类型。
  • OrderTable : this is the actual order table, we store by customer's orders. OrderTable :这是实际的订单表,我们按客户的订单存储。

I am looking for missing orderID in OrderTable .我正在寻找orderID中缺少的OrderTable

For example:例如:

Reference_Ordertable:参考_订购表:

OrderID: 1, 2, 3, 4, 5, 6, 7, 8订单编号:1、2、3、4、5、6、7、8

OrderTable:订单表:

OrderID: 1, 3, 4, 5, 7订单编号:1、3、4、5、7

I would like to find the missing part such as OrderID : 2, 6, 8 because OrderTable is missing 2,6,8 if we compare with Reference_Ordertable .我想找到缺少的部分,例如OrderID : 2, 6, 8 因为如果我们与Reference_Ordertable比较, OrderTable缺少 2,6,8。

I was thinking to use Right Join method.我正在考虑使用 Right Join 方法。 However, Right Join contains common data and it is not searching missing part.但是,Right Join 包含公共数据,并且不搜索缺失部分。 How can we filter missing data from another table?我们如何从另一个表中过滤缺失的数据?

Thanks!谢谢!

You can try below.您可以在下面尝试。

Using EXCEPT使用除了

select OrderID from reference_OrderTable
EXCEPT
select OrderID from OrderTable

using join使用加入

select r.OrderID from reference_OrderTable r 
LEFT JOIN OrderTable o ON o.OrderID = r.OrderID
WHERE o.OrderID IS NULL

using sub queries使用子查询

select OrderID from reference_OrderTable
where OrderID NOT IN (select OrderID from OrderTable)

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

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