将具有相关子查询的查询简化为简单连接
我需要帮助来简化以下查询。
我能够在下面的查询中不使用 Group By/having Count 子句但使用相关子查询来检查“0”计数。
现在,我被要求将以下查询简化为简单连接!。
我尝试将查询合并为一个。但输出不同。
您能否提出任何其他简化查询的想法,即检查“0”计数。
select distinct tab1.col1
from tab1
where tab1.col2 = 'A'
And 0 = (select count(tab2.col1)
from tab2
where tab2.col2 = 'B'
and tab2.col1 = tab1.col1)
I need help in simplifying the below query.
I was able to check for '0' count without using Group By/having Count clauses in the below query but with correlated subquery.
Now, I've been asked to simplify the below query as simple joins!.
I tried merging the query into one. But the output differs.
Could you please suggest any other idea of simplifying the query, which is checking for '0' count.
select distinct tab1.col1
from tab1
where tab1.col2 = 'A'
And 0 = (select count(tab2.col1)
from tab2
where tab2.col2 = 'B'
and tab2.col1 = tab1.col1)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这类事情通常会写成 NOT EXISTS
但是你也可以写
This sort of thing would normally be written as a NOT EXISTS
However you could also write
尝试其中一些。
如果 col1 声明为非 null,则前两个查询具有相同的执行计划(反连接)。第二种选择是我个人的建议,因为它最符合您的要求。
Try some of these.
If col1 is declared as not null, the first two queries have the same execution plan (anti-joins). The second alternative is my personal advice, since it matches your requirements the best.