【发布时间】:2008-10-09 19:30:41
【问题描述】:
我刚刚(昨天)学会了使用“exists”而不是“in”。
BAD
select * from table where nameid in (
select nameid from othertable where otherdesc = 'SomeDesc' )
GOOD
select * from table t where exists (
select nameid from othertable o where t.nameid = o.nameid and otherdesc = 'SomeDesc' )
我对此有一些疑问:
1) 我理解的解释是:“之所以这样更好,是因为只会返回匹配的值,而不是构建大量可能的结果列表”。这是否意味着虽然第一个子查询可能返回 900 个结果,但第二个子查询只会返回 1(是或否)?
2) 过去我曾抱怨过 RDBMS:“只能检索前 1000 行”,第二种方法可以解决这个问题吗?
3) 第二个子查询中别名的范围是什么?...别名是否只存在于括号中?
例如
select * from table t where exists (
select nameid from othertable o where t.nameid = o.nameid and otherdesc = 'SomeDesc' )
AND
select nameid from othertable o where t.nameid = o.nameid and otherdesc = 'SomeOtherDesc' )
也就是说,如果我在第二个“存在”中使用相同的别名( o 代表其他表),它会与第一个存在有什么问题吗?还是他们完全独立?
这是仅与 Oracle 相关还是对大多数 RDBMS 有效?
非常感谢
【问题讨论】:
-
第二个查询不解析。文本提到“第二个存在”所以你的意思是
AND exists (用于AND?
标签: sql scope table-alias