sql - 从具有超集值的表中排除子集
问题描述
表 1 有员工 ID 和代码,其中每个员工都有超集代码和子代码。
+-------------+------+
| Employee ID | Code |
+-------------+------+
| 111 | 18 |
| 111 | 19 |
| 111 | 20 |
| 111 | 21 |
| 222 | 40 |
| 222 | 41 |
| 222 | 42 |
+-------------+------+
表 2 有超集码和子集码。
+---------------+-------------+
| Superset code | Subset code |
+---------------+-------------+
| 18 | 19 |
| 18 | 20 |
| 18 | 21 |
| 40 | 41 |
| 40 | 42 |
+---------------+-------------+
我想要仅包含员工 ID 和超集代码的输出。预期结果表:
+-------------+------+
| Employee ID | Code |
+-------------+------+
| 111 | 18 |
| 222 | 40 |
+-------------+------+
如何使用 sql 查询导出此输出?
解决方案
我想你想要exists
:
select t1.*
from table1 t1
where exists (select 1 from table2 t2 where t2.supersetcode = t1.code);
出于性能考虑,您需要在table2(supersetcode)
.
推荐阅读
- mdx - MDX 多个条件过滤多个维度
- javascript - 离线移动生成 6 位代码用于 2 因素身份验证
- angular - 将 Angular 8 应用程序部署到 IIS
- xml - 一种在spring xml中将一个bean指向另一个bean的方法
- javascript - 通过javascript将特定项目移动到div元素的底部
- jquery - 动态内容的正确语法 jquery 函数是什么
- linux - insmod:错误:无法插入模块 HelloWorld.ko:不允许操作
- java - 访问 ArrayList 中的引用时出现问题
- python - 在 Python 中使用时面临的 LDAP-Login 问题
- ios - iOS 应用预售