sql - 如何将一个表的每一组与另一个表中的列进行比较?
问题描述
我有两张桌子:
类型表
TypeId PersonClassificationId
----------------------
1 1
1 2
1 3
2 1
2 2
人员分类表
PersonClassificationId Capacity
----------------------
1 2
2 2
3 2
我需要选择在整个 TypeTable 表中没有在 PersonTable 中指定的至少一个 PersonClassificationID 的TypeId。
所以,如果 PersonTable 有 1、2、3,那么应该选择 TypeId = 2,因为TypeTable 中没有记录:
TypeId PersonClassificationId
----------------------
2 3
我怎样才能做到这一点?不希望使用游标:)
解决方案
我认为你可以通过生成所有可能的类型和分类组合来做你想做的事,然后过滤映射表中不存在的那些:
select t.TypeId, pc.PersonClassificationId
from (select distinct TypeId from TypeTable) t
cross join PersonClassificationTable p
where not exists (
select 1
from TypeTable t1
where t1.TypeId = t.TypeId and t1.PersonClassificationId = p.PersonClassificationId
)
推荐阅读
- html - div内的标题位置
- php - 尝试使用 codeigniter 更新数据库中的一行只是做 +1 但似乎没有工作
- c# - 是否可以将 MetadataType 属性与接口一起使用:[MetadataType(GetType(iPerson))]
- amazon-web-services - 更新 AWS 中的 163 条 A 记录
- dax - 时间格式的十进制分钟(表格立方体)
- r - 如何从使用具有不同美学长度的不同数据创建的绘图中创建绘图网格?
- vbscript - 使用 VBS 提交 Google 表单
- latex - 乳胶书目问题,它与 natbib 一起工作正常,不适用于 biblatex
- redis - 使用 Redis、Python 和 PHP 的基于事件的邮件
- c++ - 使用 pugixml 将 xml 命名空间添加到 xml_document