【问题标题】:How is WITH used in Oracle SQL (example code shown )?在 Oracle SQL 中如何使用 WITH(显示示例代码)?
【发布时间】:2014-10-26 16:36:08
【问题描述】:

我在网上搜索“how to do an Exclusive Between oracle sql”时在网上找到了这个示例代码

Someone was proving that, in Oracle, BETWEEN 默认包含。

所以他们使用了这样的代码:

with x as (
    select 1 col1 from dual
    union
    select 2 col1 from dual
    union
    select 3 col1 from dual
    UNION
    select 4 col1 from dual

 )

 select *
   from x
  where col1 between 2 and 3

我从未见过这样的例子,WITH 是怎么回事?

【问题讨论】:

标签: sql oracle common-table-expression


【解决方案1】:

简而言之,WITH 子句是一个内联视图或子查询。当您要多次引用某个内容时,或者当您想要抽象复杂查询的部分以使其更易于阅读时,它会很有用。

如果你来自 SQL Server 世界,你也可以把它想象成一个临时表。

所以:

WITH foo as (select * from tab);

select * from foo;

就像

select * from (select * from tab);

虽然它可能更有效,因为 x 被解析为单个数据集,即使被多次查询。

它还减少了重复。如果您在一个语句中多次使用子查询,则可以考虑使用 WITH 将其分解出来。

这与 BETWEEN 示例无关,只是作者选择的演示概念的方法。

【讨论】:

    猜你喜欢
    • 2020-12-13
    • 1970-01-01
    • 1970-01-01
    • 2012-11-22
    • 2021-10-07
    • 2011-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多