SQL查询-exists和in之间的区别
以下两个查询总是给出相同的结果吗?如果没有,为什么不呢?
1)
select PayFrequencyTypeID, PayFrequencyDesc
from db_uspaybo.tblpayfrequency
where (PayFrequencyTypeID,1) not in (
select payfrequencytype,1
from tblcustomerpayapproval
where fedtaxid='903008887' and payrollyear=year(curdate())
);
2)
select payfrequencytypeid
from tblpayfrequency
where not exists (
select distinct payfrequencytype
from tblcustomerpayapproval
);
提前致谢。
Do both the following queries always give the same results? If not, why not?
1)
select PayFrequencyTypeID, PayFrequencyDesc
from db_uspaybo.tblpayfrequency
where (PayFrequencyTypeID,1) not in (
select payfrequencytype,1
from tblcustomerpayapproval
where fedtaxid='903008887' and payrollyear=year(curdate())
);
2)
select payfrequencytypeid
from tblpayfrequency
where not exists (
select distinct payfrequencytype
from tblcustomerpayapproval
);
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我想这就是你想要的“不存在”
这里的A.Id和B.Id是连接这些表的Pk和FK。(我不知道确切的字段名称)
select payFrequencyTypeID from tblpayFrequency A
不存在的地方(选择 *
来自 tblcustomerpayapproval B,其中 A.Id=B.Id 和 B.fedtaxid='903008887' 和 B.payrollyear=year(curdate()));
I think this is what you want with 'Not Exists'
A.Id and B.Id here is the Pk and FK that connect these tables.(I don't know the exact field name)
select payfrequencytypeid from tblpayfrequency A
where not exists ( select *
from tblcustomerpayapproval B where A.Id=B.Id and B.fedtaxid='903008887' and B.payrollyear=year(curdate()));
基本上,
not in
和not contains
非常相似,通常会产生相同的结果。不同之处在于,如果集合中的某个值是 NULL,
in
将返回 false(至少在 Oracle 上是这样),而exists
仅检查是否存在记录,不考虑其值。在这种特定情况下,您得到的 WHERE 子句将导致第一个查询返回不同的结果。
第三种方法通常在 MySQL 上更快,是在主查询中左连接表并检查连接字段是否为 NULL:
其他一般提示:
1
。Basically
not in
andnot exists
are very similar and usually yield the same result.A difference is that
in
will return false if one of the values in the set is NULL (at least, it does on Oracle), whileexists
only checks for existance of a record, unregarding its values.In this specific case, you got a WHERE clause that will cause the first query to return a different result.
A third approach which is generally faster on MySQL, is to left join the table in the main query and check if the join field is NULL:
Other general tips:
1
of course.简化和标记:
结果不会相同。原因:第一个查询要求 2 列。但是我们无法知道年份的问题或 903008887 的过滤是否有效果。如果该过滤有效果 - 第二个查询如何完成呢?
Simplified and tagged:
The result will not be the same. Reason: The first query asks for 2 columns. But we cannot know wheter the question for the year has some effect, or the filtering for 903008887. If that filtering has an effect - how could it be done by the second query?
第一个返回来自 db_uspaybo.tblpayFrequency 的一些数据,其中 PayFrequencyTypeID 不在具有这些条件的 payFrequencyType 表中。
第二个不从 db_uspaybo.tblpayFrequency 返回任何数据。
The first one returns the some data from db_uspaybo.tblpayfrequency where their PayFrequencyTypeID is not in the payfrequencytype table with those conditions.
The second one does not return any data from db_uspaybo.tblpayfrequency.
请查看以下博客:
IN 与 Exist在 SQL 中
或者,您始终可以通过谷歌搜索此类问题。
Look at the following blog:
IN Vs Exist in SQL
Alternatively you can always google for such type of questions.