当前位置:网站首页>[database] CTE (common table expression)
[database] CTE (common table expression)
2022-07-26 06:53:00 【Average again】
Common table expression (CTE) The definition of
The definition of a common expression consists of three parts :
Name of common table expression ( stay WITH after )
Column names involved ( Optional )
One SELECT sentence ( Following the AS after ),
One of the benefits of common table expressions is how long they can be referenced in the next statement , Remember the next sentence
stay MSDN Prototype in :
WITH expression_name [ ( column_name [,...n] ) ]
AS
( CTE_query_definition )
Non recursive common table expression (CTE)
Nonrecursive CTE Quotation and common Select There is not much substantive difference between queries , Call the following :
with admin_CET
as
(
select ID,name,parentid from adminpart
)
select ID,name,parentid from admin_CET
Non recursive CTE The most common place to call is paging query :
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
recursive CTE call
Refer to CTE Call itself in the statement in it. CTE.CTE Derived recursion can be defined more than once . The concept of recursion , A function or procedure calls itself directly or indirectly ;
CTE The common place for recursive calls is that a table contains ownership , Query the sub data of the current data ;
Simply define a table :
CREATE TABLE [dbo].[adminpart](
[ID] [int] IDENTITY(1,1) NOT NULL,-- At present ID
[name] [nvarchar](50) NOT NULL,-- full name
[parentid] [int] NOT NULL-- Parent class ID
)
The data are as follows 
Inquire about admin All users under , At this time, we need to use the concept of recursion to complete :
with admin_CET
as
(
select ID,name,parentid,0 as alv from adminpart where name='admin'-- Basic query
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 -- recursive join Inquire about , Note that it must be ap.parentid=ac.ID, If you don't understand , We need to understand the concept of recursion
)
CTE Appearance , It is a good solution to the query of mutual primary and secondary relationship data types . notes :SQL2003 I won't support it CTE.
remarks :
recursive CTE Contain at least two queries ( Also known as member ). The first query is fixed-point members , A fixed-point member is just a query that returns a valid table , A base or anchor for recursion . The second query is called recursive member , What makes this query a recursive member is a pair of CTE A recursive reference to a name is a trigger . Logically CTE The internal application of the name is understood as the result set of the previous query .
Recursive queries have no explicit recursive termination conditions , Stop recursion only if the second recursive query returns an empty result set or exceeds the maximum number of recursions . The method that refers to the upper limit of the number of recursions is to use MAXRECURION.
Mysql The syntax of recursion is a little different
It's mainly more RECURSIVE, Auspicious view :【 note 】MYSQL8 new function CTE( Common table expressions ) Elegant implementation of recursive query
边栏推荐
- Heap sort
- IV Actual combat - global unified return result class
- 深度学习——CV、CNN、RNN
- Use scanner to get multiple data types from the keyboard
- 『牛客|每日一题』有效括号序列
- Differences and relations between varchar and nvarchar in database
- shell编程
- [Star Project] small hat aircraft War (IV)
- 『HarmonyOS』探索HarmonyOS应用
- < II> ObjectARX development: create and edit basic graphic objects
猜你喜欢

二叉树知识总结

The results of the soft test can be checked, and the entry to query the results of the soft test has been opened in the first half of 2022

Children's programming electronic society graphical programming level examination scratch level 1 real problem analysis (multiple choice) June 2022

vulnhub Lampião: 1

TCP protocol -- message format, connection establishment, reliable transmission, congestion control
![Rust语言- Slice(切片)类型(&[u8])](/img/d1/68c73c8b34b848212083c08df3137f.png)
Rust语言- Slice(切片)类型(&[u8])

Introduce you to JVM from architecture

"Niuke | daily question" template stack

快速排序(quick-sort)

Download, installation and development environment construction of "harmonyos" deveco
随机推荐
XSS labs (1-10) break through details
Open includeexceptiondetailinfaults on the server (from servicebehaviorattribute or from & lt; servicedebug & gt; to configure behavior) to send exception information back
buuReserve(4)
Rectification ideas for the previous article
[image denoising] image denoising based on bicube interpolation and sparse representation matlab source code
C # use log4net plug-in to output logs to files
[untitled] reprint
深度学习——CV、CNN、RNN
打开服务器上的 IncludeExceptionDetailInFaults (从 ServiceBehaviorAttribute 或从 &lt;serviceDebug&gt; 配置行为)以便将异常信息发送回
字符串和内存函数
vulnhub Lampião: 1
[graduation season _ advanced technology Er] farewell to yourself who has been confused for the past two years. Regroup, junior I'm coming
『牛客|每日一题』点击消除
CS5801_HDMI转EDP优势替代LT6711A方案
曲线曲率展示
Tiktok web s_ v_ web_ Analysis and implementation of ID parameter generation
服装行业如何实现数字化生产模式
二叉树知识总结
Curve curvature display
How strong is the working ability of a project manager with a monthly salary of 50000?