简体   繁体   English

比较表并排除值

[英]Comparing the tables and excluding the values

I have two schemas named as "t3" and "r1".我有两个名为“t3”和“r1”的模式。 Both have the columns "Permission".两者都有“权限”列。 Through sql query;通过sql查询; I want to Get the list of all permissions from "t3" and excludes the permission from "r1".我想从“t3”获取所有权限的列表并排除“r1”的权限。 I am not really into sql that's why it is confusing me a lot.我不是很喜欢 sql 这就是为什么它让我很困惑。

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

In general, it looks like you need something like that.一般来说,看起来你需要这样的东西。 Although the question is not entirely clear.虽然问题并不完全清楚。 This is not the most productive option, if there is a lot of data, you can rewrite it in join and it will be more correct.这不是最高效的选择,如果有很多数据,你可以在 join 中重写它,它会更正确。

I would recommend not exists : it seems like a straight-forward approach to your querstion;我建议not exists :这似乎是对您的问题的直接方法; also it usually scales better than [not] in when the number of row increases - and it is null-safe (as opposed to not in ):当行数增加时,它通常比[not] in更好地扩展 - 并且它是空安全的(与not in相对):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM