【问题标题】:Strange SQL execution result in Oracle [closed]Oracle中奇怪的SQL执行结果[关闭]
【发布时间】:2012-07-20 08:13:10
【问题描述】:
select unique owner 
from all_tables 
where sysdate-50 < (select last_analyzed from dual);

我刚刚写了上面的代码,奇怪的是结果与下面的代码不同。

select unique owner from all_tables;

但是如果我单独执行(select last_analyzed from dual),就会弹出错误。

我很困惑结果是如何产生的。

【问题讨论】:

  • 您遇到的错误是什么?
  • LAST_ANALYZED 是存在于子查询上下文(ALL_TABLES 系统视图)中而不是 DUAL 系统表中的列。伟大的APC! +1 给你

标签: sql oracle subquery


【解决方案1】:

它将last_analysed 作为all_tables 的一列,因为dual 中没有这样的列——我想这是范围的影响。如果用别名写就更清楚了:

select unique owner
from all_tables t
where sysdate-50 < (select t.last_analyzed from dual);

你根本不需要子查询,你可以这样做:

select unique owner
from all_tables
where last_analyzed >= sysdate-50;

(我怀疑这是错误的方法;如果您正在寻找陈旧的统计数据,我假设您想要&lt; sysdate-50)。

【讨论】:

    【解决方案2】:

    您的查询有一个不必要的子查询。这是等价的:

    select unique owner from all_tables T
    where sysdate-50 < T.last_analyzed;
    

    【讨论】:

    • +1 但这不会执行,除非我删除'as'
    • 谢谢。我自己从不使用AS,但不记得Oracle 是否需要它。固定。
    【解决方案3】:

    last_analyzed 是 all_tables 中具有日期值的列之一。你不能单独运行子查询

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-10
      • 1970-01-01
      • 2023-01-16
      • 1970-01-01
      • 2017-10-29
      相关资源
      最近更新 更多