sql - 在 SQL 查询中执行 MINUS 时忽略列
问题描述
我已经写了查询:
Select distinct a,b from t1 minus Select distinct a,b from t2.
这里 t1 和 t2 是两个表。我想要在 t1 但不在 t2 中出现的 a 和 b 的不同值。所以我使用减号运算符。我想要 a 和 b 的值,但我知道在某些情况下, t1 和 t2 中的 b 值可能不同。这将导致 t1 和 t2 中都存在 a 和 b 的值,因为如果 b 的值在两个表中都不匹配,则不会发生减号。我怎样才能成功地做到这一点?
如何获取表 t1 中存在但表 t2 中不存在的 a 和 b 的值,即使在某些情况下 b 的值可能在两个表中都不匹配?
table1: table2:
column1 column2 column1 column2
1 a 1 c
2 b 3 d
在这种情况下,我只想要值 (2,b)。我不希望 (1,a) 因为 1 也出现在 table2 中。
解决方案
开始not exists
:
select distinct. . .
from t1
where not exists (select 1 from t2 where t2.a = t1.a and t2.b = t1.b);
根据您的描述,您可能只想比较a
:
select distinct a, b
from t1
where not exists (select 1 from t2 where t2.a = t1.a);
推荐阅读
- javascript - 在 FaltList React Native 中仅显示 10 条记录
- php - 将PHP函数转换为linux bash?
- python - 测试返回 json 响应的 Flask 端点的最佳方法
- python - 随机生成的平台似乎是空心的(Python - pygame)
- python - Python 克隆 git 存储库的方法
- java - 即使将数据集更改为另一个值,哈希也不会更改
- batch-file - 尝试通过 cmd 从 PuTTY 执行命令时如何忽略/抑制错误
- bash - 如何找到并用引号替换字符串?
- reactjs - 在 Reactjs 中单击按钮将数据添加到数组
- c++ - CreateProcess 退出后占用套接字端口(每个套接字地址只允许使用一次)