【sql】sql中with as 介绍与使用jsqlparser解析sql

一. 定义

with A as (select * from class)

也就是将重复用到的大批量 的SQL语句,放到with as 中,加一个别名,在后面用到的时候就可以直接用。对于大批量的SQL数据,起到优化的作用。

with子句的返回结果存到用户的临时表空间中,只做一次查询,反复使用,提高效率。

 

二. 用法

  1. with子句只能被select查询块引用
  2. 在同级select前有多个查询定义的时候,第1个用with,后面的不用with,并且用逗号隔开。
  3. 最后一个with 子句与下面的查询之间不能有逗号,只通过右括号分割,with 子句的查询必须用括号括起来。

-- 针对一个别名
-- –相当于建了个e临时表

with e as (select * from scott.emp e where e.empno=7499)
select * from e;

-- –针对多个别名,相当于建了e、d临时表

with
e as (select * from scott.emp),
d as (select * from scott.dept)
select * from e, d where e.deptno = d.deptno;

 

三. 解析


<dependency>  
  <groupId>com.github.jsqlparser</groupId>  
  <artifactId>jsqlparser</artifactId>  
  <version>4.9</version>  
</dependency>

官网文档:

https://jsqlparser.github.io/JSqlParser/usage.html#parse-a-sql-statements

//去除 with语法下的别名
private static Set<String> removeWithAlias(String sql, Set<String> tables) {  
    if (sql.contains("with ")) {  
        PlainSelect select = null;  
        try {  
            select = (PlainSelect) CCJSqlParserUtil.parse(sql);  
            List<WithItem> withItemsList = select.getWithItemsList();  
            List<String> withAlias = withItemsList.stream()  
                    .map(withItem -> withItem.getAlias().getName())  
                    .collect(Collectors.toList());  
            return tables.stream().filter(t -> !withAlias.contains(t))  
                    .collect(Collectors.toSet());  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
    return tables;  
}

 

相关推荐

  1. sqlsqlwith as 介绍使用jsqlparser解析sql

    2024-05-15 22:46:20       28 阅读
  2. SQL WITH AS 的使用方法

    2024-05-15 22:46:20       59 阅读
  3. SQLNULL值比较问题解析解决方法

    2024-05-15 22:46:20       38 阅读
  4. sql WITH CTE AS 用法

    2024-05-15 22:46:20       42 阅读
  5. SQLWITH RECURSIVE的用法

    2024-05-15 22:46:20       39 阅读
  6. SQL 解析执行流程

    2024-05-15 22:46:20       54 阅读
  7. 深入了解Flutter的Sliver:介绍使用场景

    2024-05-15 22:46:20       64 阅读

最近更新

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

    2024-05-15 22:46:20       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-15 22:46:20       106 阅读
  3. 在Django里面运行非项目文件

    2024-05-15 22:46:20       87 阅读
  4. Python语言-面向对象

    2024-05-15 22:46:20       96 阅读

热门阅读

  1. AIGC行业现在适合进入吗

    2024-05-15 22:46:20       30 阅读
  2. 网络工程师----第二十九天

    2024-05-15 22:46:20       34 阅读
  3. 拓展(华为优秀网站)

    2024-05-15 22:46:20       32 阅读
  4. Qt | QTimer 类(计时器)

    2024-05-15 22:46:20       39 阅读
  5. vld.ini配置文件说明

    2024-05-15 22:46:20       34 阅读
  6. 基础环境配置

    2024-05-15 22:46:20       35 阅读
  7. LabVIEW软件开发工程师需要具备哪些能力与素质?

    2024-05-15 22:46:20       37 阅读
  8. 总结_看门狗项目应用解析

    2024-05-15 22:46:20       29 阅读
  9. Docker 容器连接:构建安全高效的容器化网络生态

    2024-05-15 22:46:20       33 阅读
  10. mysql(二)

    2024-05-15 22:46:20       32 阅读
  11. mysql中exists和in的区别

    2024-05-15 22:46:20       35 阅读
  12. MySQL变量的定义与使用

    2024-05-15 22:46:20       29 阅读