作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有 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/
我是一名优秀的程序员,十分优秀!