比较表并排除值

Comparing the tables and excluding the values

我有两个模式,分别命名为“t3”和“r1”。两者都有“权限”列。通过sql查询;我想从“t3”获取所有权限的列表并排除“r1”的权限。我不是很喜欢 sql 这就是为什么它让我很困惑。

select *
from t3 
where t3.Permission not in (select Permission from r1)

总的来说,您似乎需要这样的东西。虽然问题并不完全清楚。 这不是最有效率的选择,如果有很多数据,你可以在join中重写它会更正确。

我会推荐 not exists:这似乎是 straight-forward 解决您问题的方法;当行数增加时,它通常比 [not] in 更好地扩展 - 它是 null-safe(与 not in 相反):

select t3.permissions
from t3
where not exists (select 1 from r1 where r1.permissions = t3.permissions)