使用 NOT EXISTS 子句编写查询,而没有 NOT EXISTS 的子查询 [英] Writing a query with a NOT EXISTS clause without a subquery for the NOT EXISTS
问题描述
我有兴趣为应用程序编写查询,在该应用程序中我需要使用 NOT EXISTS
子句来检查行是否存在.
I was interested in writing a query for an application where I need to use a NOT EXISTS
clause to check if a row exists.
我正在使用 Sybase,但我想知道是否有 SQL 中的一般示例,您可以在其中编写具有 NOT EXISTS
子句的查询,而没有 NOT 的嵌套子查询存在
.
I am using Sybase but I would like to know if there is an example in SQL in general where you can write a query having a NOT EXISTS
clause without a nested subquery for the NOT EXISTS
.
所以代替
SELECT * FROM TABLE
WHERE NOT EXISTS (SOME SUBQUERY)
有没有办法在没有子查询的情况下编写这个?
is there a way to write this without a subquery?
编辑:不幸的是,我无法向您展示查询,因为它是机密信息,但我可以解释的是我正在尝试这样做:
EDIT: unfortunately, I cannot show you the query since it is confidential information but what I can explain is that I am trying to do this:
SELECT t1.a
FROM (select t2.a from table t2 ,table t3 where t2.b = t3.b ) as t1
where not exists (select t1.a from table t1 )
希望清楚.
推荐答案
您可以使用 LEFT JOIN 而不是 EXISTS 来编写反联接:
You could write an anti-join using LEFT JOIN instead of an EXISTS:
SELECT t1.*
FROM Table1 t1
LEFT JOIN Table2 t2
ON t2.Id = t1.Id
WHERE t2.Id IS NULL
但是对于 EXISTS 操作符,你必须有一个子查询.
But with the EXISTS operator, you must have a subquery.
这篇关于使用 NOT EXISTS 子句编写查询,而没有 NOT EXISTS 的子查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!