Oracle with as用法

一、简介

with…as关键字,是以‘with’关键字开头的sql语句,在实际工作中,我们经常会遇到同一个查询sql会同时查询多个相同的结果集,即sql一模一样,这时候我们可以将这些相同的sql抽取出来,使用with…as定义。with…as相当于一张中间表,可以简单理解为sql片段(类似java复用代码)。

下面我们通过两个简单的示例说明with…as的使用方法。

二、使用方法

【a】多次使用抽取出来

–with as 可以理解为一张临时表或者理解成sql片段,在多次查询语句相同的时候可以抽取出来,达到’一次解析,多次使用’
–如果每个部分都去执行一遍的话,则成本比较高,可以使用with as短语,则只要执行一遍即可

with temp as
 (select '10001' as province_code from dual)
 
select case
         when (select * from temp) = '10001' then
          'equals'
         when (select * from temp) = '10002' then
          'not equals'
         else
          'unknown'
       end is_equals
  from dual;

img

【b】在union 语句中使用

--with as 非常适合在union 语句中
--注意:with as 语句最后面不能加分号,否则报缺失select关键字错误。
with temp1 as
 (select 'female' sex, 'zhangsan' stu_name from dual),
temp2 as
 (select 'male' sex, 'lisi' stu_name from dual),
temp3 as
 (select 'female' sex, 'wangwu' stu_name from dual)
 
select *
  from temp1
union all
select *
  from temp2
union all
select * from temp3

在这里插入图片描述

在这里插入图片描述

注意点:with…as后面不能加分号,否则报错。

【c】注意点:

  1. 不能只定义with…as语句,定义了要使用它,否则会报错
--只定义with..as会报错
with temp1 as
 (select 'female' sex, 'zhangsan' stu_name from dual),
temp2 as
 (select 'male' sex, 'lisi' stu_name from dual)

在这里插入图片描述

  1. 前面定义的with…as语句可以在后面定义的with…as语句中使用
--前面定义的with..as语句可以在后面定义的with..as语句使用
with temp1 as
 (select 'female' sex, 'zhangsan' stu_name from dual),
temp2 as
 (select 'male' sex, 'lisi' stu_name from dual),
temp3 as
 (select * from temp2)
 
select *
  from temp1
union all
select *
  from temp2
union all
select * from temp3

在这里插入图片描述

三、总结

with…as其实就是将经常需要查询的语句抽取出来,形成一个虚拟表,我们后面可以多次使用,达到‘一次解析,多次使用’的效果,大大提高执行的效率。

相关推荐

  1. new Promise

    2024-03-15 09:38:01       47 阅读
  2. qt 定时器

    2024-03-15 09:38:01       59 阅读
  3. fmt

    2024-03-15 09:38:01       57 阅读
  4. not exists

    2024-03-15 09:38:01       58 阅读
  5. 详解WebMvcConfigurer

    2024-03-15 09:38:01       41 阅读
  6. Tinyxml基本

    2024-03-15 09:38:01       62 阅读
  7. man

    2024-03-15 09:38:01       55 阅读
  8. mybatisPlus 常见

    2024-03-15 09:38:01       45 阅读
  9. v-show

    2024-03-15 09:38:01       59 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-15 09:38:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-15 09:38:01       101 阅读
  3. 在Django里面运行非项目文件

    2024-03-15 09:38:01       82 阅读
  4. Python语言-面向对象

    2024-03-15 09:38:01       91 阅读

热门阅读

  1. 本地环境下运行Spark程序

    2024-03-15 09:38:01       43 阅读
  2. Python和MATLAB数字信号波形和模型模拟

    2024-03-15 09:38:01       45 阅读
  3. 90%的程序员不适合做独立开发

    2024-03-15 09:38:01       41 阅读
  4. 这个不需要吗 HttpServletRequest req

    2024-03-15 09:38:01       48 阅读
  5. 2024.3.14

    2024.3.14

    2024-03-15 09:38:01      40 阅读
  6. 深度学习中的“张量”怎么理解呢?

    2024-03-15 09:38:01       35 阅读
  7. 基于深度学习的人体姿态估计

    2024-03-15 09:38:01       46 阅读
  8. 【leetcode题解C++】146. LRU缓存

    2024-03-15 09:38:01       45 阅读
  9. 读深度学习的一些论文

    2024-03-15 09:38:01       44 阅读
  10. perl 用 XML::LibXML 解析 Freeplane.mm文件,

    2024-03-15 09:38:01       38 阅读