首页 > 解决方案 > SQL 查询以考虑当前表中的行并排除其他表中存在的数据

问题描述

我在 Postgres 数据库中有两个表。

Table A
{
userid1
userid2
count
}

Table B
{
userid1
userid2
}

我正在尝试获取除表 B 中具有相同 userid1 和 userid2 的数据之外的所有数据。以下查询仅提供两列,但我也需要表 A 中的第三列。

Select userid1,userid2 from A except (select * from B)

请帮我解决这个问题。谢谢您的帮助

标签: sqlpostgresql

解决方案


使用not exists

select a.*
from a
where not exists (select 1
                  from b
                  where b.userid1 = a.userid1 and b.userid2 = a.userid2
                 );

推荐阅读