【发布时间】:2022-01-18 05:47:18
【问题描述】:
我有两张桌子:
| acc_num | ser_code |
|---|---|
| 1 | A |
| 1 | B |
| 1 | C |
| 2 | C |
| 2 | D |
第二个是:
| ser_code | value |
|---|---|
| A | 5 |
| B | 8 |
| C | 10 |
| D | 15 |
我想排除服务代码值为 10 或 15 的所有帐户。 因为我的数据集很大,我想使用 NOT EXIST 但它只是排除了 acc_num 和 ser_code 的组合。 我想用所有的 ser_code 排除 acc_num,因为它的 ser_code 符合我的标准。
我用过:
选择 acc_num, ser_code
从表 1
不存在的地方(选择 1
FROM 表 2 其中 acc_num = acc_num 和 (10, 15) 中的值
上面的代码输出是:
| acc_num | ser_code |
|---|---|
| 1 | A |
| 1 | B |
输出的愿望是空的。
【问题讨论】:
-
您使用的是哪个 rdms?
-
Oracle 和 Microsoft SQL Server 数据库
-
select distinct acc_num from table2 where value in (10, 15)是否返回您要过滤掉的 acc_num?然后很容易将其合并到您的查询中:where acc_num not in (select distinct acc_num from table2 where value in (10, 15))
标签: sql not-exists exclude