gpt4 book ai didi

sql - 在表 C 中不存在的表 B 中查找表 A 中缺失的记录

转载 作者:行者123 更新时间:2023-12-04 22:42:13 25 4
gpt4 key购买 nike

我有 3 个表:

Customer (CustomerID)
CustomerEvent (CustomerEventID, CustomerID, EventTypeID)
EventType (EventTypeID)

一些客户记录有一些带有 EventType 的 CustomerEvent 记录,一些客户记录没有 CustomerEvent 记录。

如何为每个客户记录识别/插入每个事件类型的缺失客户事件记录?

我的实际问题比这更详细,但是,这是我正在努力解决的问题。

我可以使用一个选择语句来识别所有缺失的 CustomerEvent 记录吗?或者我是否需要对每个 EventType 记录进行 UNION?

最佳答案

使用cross join生成所有CustomerId,EventTypeId的集合,过滤掉CustomerEvent中存在的,不存在的()

select c.CustomerId, e.EventTypeId
from Customer c
cross join EventType e
where not exists (
select 1
from CustomerEvent ce
where c.CustomerId = ce.CustomerId
and e.EventTypeId = ce.EventTypeId
)

关于sql - 在表 C 中不存在的表 B 中查找表 A 中缺失的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44442973/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com