当前位置:网站首页>【数据库】CTE(Common Table Expression(公共表表达式))
【数据库】CTE(Common Table Expression(公共表表达式))
2022-07-26 06:50:00 【又被平均了】
公用表表达式(CTE)的定义
公用表达式的定义包含三部分:
公用表表达式的名字(在WITH之后)
所涉及的列名(可选)
一个SELECT语句(紧跟AS之后),
公用表表达式的好处之一是可以在接下来一条语句中多长引用,记得是接下来一条语句
在MSDN中的原型:
WITH expression_name [ ( column_name [,...n] ) ]
AS
( CTE_query_definition )
非递归公用表表达式(CTE)
非递归的CTE引用与普通Select查询没有太实质性的区别,调用如下:
with admin_CET
as
(
select ID,name,parentid from adminpart
)
select ID,name,parentid from admin_CET
非递归CTE调用比较常用的地方就是分页查询:
with admin_CET
as
(
select ID,name,parentid,row_number() over(order by ID) as rowNum from adminpart
)
select ID,name,parentid,rowNum from admin_CET where rowNum between 1 and 10
递归CTE调用
指的是在CTE内的语句中调用其自身的CTE。CTE可以在一次定义多次进行派生递归。对于递归的概念,是指一个函数或是过程直接或者间接的调用其自身;
CTE递归调用常用的地方是一个表中含有所属关系,查询所属当前数据的子数据;
简单定义一个表:
CREATE TABLE [dbo].[adminpart](
[ID] [int] IDENTITY(1,1) NOT NULL,--当前ID
[name] [nvarchar](50) NOT NULL,--姓名
[parentid] [int] NOT NULL--父类ID
)
数据如下
查询admin下的所有用户,这个时候就需要要用递归的概念来完成:
with admin_CET
as
(
select ID,name,parentid,0 as alv from adminpart where name='admin'--基础查询
union all
select ap.ID,ap.name,ap.parentid,alv+1 as alv from admin_CET ac inner join adminpart ap
on ap.parentid=ac.ID --递归join查询,注意一定是ap.parentid=ac.ID,如果不理解,就要搞明白递归的概念
)
CTE的出现,很好的解决了互为主子关系数据类型的查询。注:SQL2003不支持CTE。
备注:
递归CTE最少包含两个查询(也被称为成员)。第一个查询为定点成员,定点成员只是一个返回有效表的查询,用于递归的基础或定位点。第二个查询被称为递归成员,使该查询称为递归成员的是对CTE名称的递归引用是触发。在逻辑上可以将CTE名称的内部应用理解为前一个查询的结果集。
递归查询没有显式的递归终止条件,只有当第二个递归查询返回空结果集或是超出了递归次数的最大限制时才停止递归。是指递归次数上限的方法是使用MAXRECURION。
Mysql递归的语法有点差异
主要多了RECURSIVE,祥见:【笔记】MYSQL8新功能CTE(公共表表达式)优雅实现递归查询
边栏推荐
- buuReserve(4)
- Can C use reflection to assign values to read-only attributes?
- How to solve the crash when the easygbs platform edits the device management group?
- 原生高性能抓包工具Proxyman,送给爱学习的你
- CONDA virtual environment envs directory is empty
- Celery takes up large memory - memory leak
- <二> objectARX开发:创建和编辑基本图形对象
- IV Actual combat - global unified return result class
- Downloadutilse tool class without error
- 【毕业季_进击的技术er】送别过去两年迷茫的自己。重整旗鼓,大三我来啦
猜你喜欢

『牛客|每日一题』点击消除

The "darkest hour" has not come yet. Cherish every bullet 2020-03-22

Integrated design of signal processing systems - Design of solver functions (continuous and discrete time systems)

Address resolution ARP Protocol

Binary tree knowledge summary
![[hardware ten treasures] - 7.1 [dynamic RAM] key points of DDR hardware design](/img/ba/87cd3b1600bcb6f2839e7bb093ff62.png)
[hardware ten treasures] - 7.1 [dynamic RAM] key points of DDR hardware design

vulnhub Lampião: 1
![[image denoising] image denoising based on bicube interpolation and sparse representation matlab source code](/img/39/716c62d6ca533a7e84704b2c55d072.png)
[image denoising] image denoising based on bicube interpolation and sparse representation matlab source code

Huffman coding principle
![Rust language - slice type (&[u8])](/img/d1/68c73c8b34b848212083c08df3137f.png)
Rust language - slice type (&[u8])
随机推荐
Integrated design of signal processing systems - Design of solver functions (continuous and discrete time systems)
UIToolkit工具模板工程
SQL optimization scheme
String and memory functions
Development stage of source code encryption technology
【无标题】转载
[Web3 series development tutorial - create your first NFT (4)] what can NFTs bring to you
"Harmonyos" explore harmonyos applications
『牛客|每日一题』逆波兰表达式
打开服务器上的 IncludeExceptionDetailInFaults (从 ServiceBehaviorAttribute 或从 <serviceDebug> 配置行为)以便将异常信息发送回
@Constructorproperties annotation understanding and its corresponding usage
MySQL基础篇(二)-- MySQL 基础
XSS labs (1-10) break through details
使用Scanner从键盘获取多种数据类型
软考可以查成绩了,2022年上半年软考成绩查询入口已开通
微信小程序 - 从入门到入土
28. Implement strstr() implement strstr()
Is the passenger flow always low? There is something wrong with the location of your store!
Esxi 7.0 installation supports mellanox technologies mt26448 [connectx en 10gige, PCIe 2.0 5gt/s] driver, and supports the cheapest 10GB dual fiber network card
Children's programming electronic society graphical programming level examination scratch level 1 real problem analysis (multiple choice) June 2022