开发者社区 问答 正文

SQL-表别名范围

我刚刚学会了(昨天)使用“存在”而不是“输入”。

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' )
也就是说,如果我使用相同的别名(对于表othertable的表为o),则在第二个“存在”中,第一个存在是否存在任何问题?还是他们完全独立?

这是Oracle唯一相关的东西,还是对大多数RDBMS有效?

非常感谢

问题来源于stack overflow

展开
收起
保持可爱mmm 2019-11-18 15:58:48 537 分享 版权
1 条回答
写回答
取消 提交回答
  • 它特定于每个DBMS,并取决于查询优化器。一些优化器检测IN子句并将其翻译。

    在我测试过的所有DBMS中,别名仅在()内部有效

    顺便说一句,您可以将查询重写为:

    select t.* from table t join othertable o on t.nameid = o.nameid and o.otherdesc in ('SomeDesc','SomeOtherDesc'); 并且,回答您的问题:

    是 是 是

    2019-11-18 15:59:08
    赞同 展开评论
问答分类:
问答标签:
问答地址: