sql - SQL 查询以考虑当前表中的行并排除其他表中存在的数据
问题描述
我在 Postgres 数据库中有两个表。
Table A
{
userid1
userid2
count
}
Table B
{
userid1
userid2
}
我正在尝试获取除表 B 中具有相同 userid1 和 userid2 的数据之外的所有数据。以下查询仅提供两列,但我也需要表 A 中的第三列。
Select userid1,userid2 from A except (select * from B)
请帮我解决这个问题。谢谢您的帮助
解决方案
使用not exists
:
select a.*
from a
where not exists (select 1
from b
where b.userid1 = a.userid1 and b.userid2 = a.userid2
);
推荐阅读
- javascript - 如何禁用带有highcharts的图表显示?
- java - OnError 观察 Observable on Retrofit
- swiftui - 从 SwiftUI 中的上下文菜单触发导航
- hive - 从 Hive 中的 Metastore 查询表抛出错误
- angularjs - 如何在同一页面上使用 ion-menu 和 ion-slides?
- angular - 覆盖排序表的角度材料默认样式
- python - 如何修复“溢出错误:编码字符串时不支持的 UTF-8 序列长度”
- arrays - Spark - 从嵌套数组中的结构中选择列时出错
- powershell - 批量调用powershell时如何在一行中回显变量和字符串
- python - 如何指定与多个相似表的关系