最近研究了一下CTE递归查询,感觉这个CTE递归查询蛮好用的,在网上找到了一个比较好的例子,测试例子如下 1 use City; 2 go 3 create table Tree 4 ( 5 ID int identity ( 1 , 1 ) primary key not null , 6 Name varchar ( 20 ) not null , 7 Parent varcha
最近研究了一下CTE递归查询,感觉这个CTE递归查询蛮好用的,在网上找到了一个比较好的例子,测试例子如下
<span> 1</span> <span>use</span><span> City; </span><span> 2</span> <span>go</span> <span> 3</span> <span>create</span> <span>table</span><span> Tree </span><span> 4</span> <span>( </span><span> 5</span> ID <span>int</span> <span>identity</span>(<span>1</span>,<span>1</span>) <span>primary</span> <span>key</span> <span>not</span> <span>null</span><span>, </span><span> 6</span> Name <span>varchar</span>(<span>20</span>) <span>not</span> <span>null</span><span>, </span><span> 7</span> Parent <span>varchar</span>(<span>20</span>) <span>null</span> <span> 8</span> <span>) </span><span> 9</span> <span>go</span> <span>10</span> <span>insert</span> Tree <span>values</span>(<span>'</span><span>大学</span><span>'</span>,<span>null</span><span>) </span><span>11</span> <span>insert</span> Tree <span>values</span>(<span>'</span><span>学院</span><span>'</span>,<span>'</span><span>大学</span><span>'</span><span>) </span><span>12</span> <span>insert</span> Tree <span>values</span>(<span>'</span><span>计算机学院</span><span>'</span>,<span>'</span><span>学院</span><span>'</span><span>) </span><span>13</span> <span>insert</span> Tree <span>values</span>(<span>'</span><span>网络工程</span><span>'</span>,<span>'</span><span>计算机学院</span><span>'</span><span>) </span><span>14</span> <span>insert</span> Tree <span>values</span>(<span>'</span><span>信息管理</span><span>'</span>,<span>'</span><span>计算机学院</span><span>'</span><span>) </span><span>15</span> <span>insert</span> Tree <span>values</span>(<span>'</span><span>电信学院</span><span>'</span>,<span>'</span><span>学院</span><span>'</span><span>) </span><span>16</span> <span>insert</span> Tree <span>values</span>(<span>'</span><span>教务处</span><span>'</span>,<span>'</span><span>大学</span><span>'</span><span>) </span><span>17</span> <span>insert</span> Tree <span>values</span>(<span>'</span><span>材料科</span><span>'</span>,<span>'</span><span>教务处</span><span>'</span><span>) </span><span>18</span> <span>insert</span> Tree <span>values</span>(<span>'</span><span>招生办</span><span>'</span>,<span>'</span><span>大学</span><span>'</span><span>) </span><span>19</span> <span>go</span> <span>20</span> <span>with</span> CTE <span>as</span> <span>21</span> <span>( </span><span>22</span> <span>--</span><span>>Begin 一个定位点成员</span> <span>23</span> <span>select</span> ID, Name,Parent,<span>cast</span>(Name <span>as</span> <span>nvarchar</span>(<span>max</span>)) <span>as</span> TE,<span>0</span> <span>as</span> Levle <span>from</span> Tree <span>where</span> Parent <span>is</span> <span>null</span> <span>24</span> <span>--</span><span>>End </span> <span>25</span> <span>union</span> <span>all</span> <span>26</span> <span>--</span><span>>Begin一个递归成员</span> <span>27</span> <span>select</span> Tree.ID, Tree.Name,Tree.Parent,<span>cast</span>(<span>replicate</span>(<span>'</span> <span>'</span>,<span>len</span>(CTE.TE))<span>+</span><span>'</span><span>|_</span><span>'</span><span>+</span>Tree.name <span>as</span> <span>nvarchar</span>(<span>MAX</span>)) <span>as</span> TE,Levle<span>+</span><span>1</span> <span>as</span><span> Levle </span><span>28</span> <span>from</span> Tree <span>inner</span> <span>join</span><span> CTE </span><span>29</span> <span>on</span> Tree.Parent<span>=</span><span>CTE.Name </span><span>30</span> <span>--</span><span>>End</span> <span>31</span> <span>) </span><span>32</span> <span>select</span> <span>*</span> <span>from</span> CTE <span>order</span> <span>by</span><span> ID </span><span>33</span> <span>--</span><span>1.将 CTE 表达式拆分为定位点成员和递归成员。</span> <span>34</span> <span>--</span><span>2.运行定位点成员,创建第一个调用或基准结果集 (T0)。</span> <span>35</span> <span>--</span><span>3.运行递归成员,将 Ti 作为输入(这里只有一条记录),将 Ti+1 作为输出。</span> <span>36</span> <span>--</span><span>4.重复步骤 3,直到返回空集。</span> <span>37</span> <span>--</span><span>5.返回结果集。这是对 T0 到 Tn 执行 UNION ALL 的结果。</span>
上面的SQL语句再次插入一条数据:
insert Tree values('网络1班','网络工程')
运行结果如下图:
图1 运行结果
注意点:貌似在递归成员处所选择的字段都必须Tree表的数据,而不能是CTE结果集中的除了Tree中没有而CTE中有的字段在这里才可以引用,比如字段TE。
首先看下,遍历的第1条记录的SQL语句:
<span>1</span> <span>select</span> ID, Name,Parent,<span>cast</span>(Name <span>as</span> <span>nvarchar</span>(<span>max</span>)) <span>as</span> TE,<span>0</span> <span>as</span> Levle <span>from</span> Tree <span>where</span> Parent <span>is</span> <span>null</span>
获取的结果为:
Name Parent TE Levle
-------------------------------------
大学 NULL 大学 0
递归第2次所获取的结果集合的类SQL语句为:
<span>1</span> <span>select</span> Tree.ID, Tree.Name,Tree.Parent,<span>cast</span>(<span>replicate</span>(<span>'</span> <span>'</span>,<span>len</span>(CTE.TE))<span>+</span><span>'</span><span>|_</span><span>'</span><span>+</span>Tree.name <span>as</span> <span>nvarchar</span>(<span>MAX</span>)) <span>as</span> TE,Levle<span>+</span><span>1</span> <span>as</span><span> Levle </span><span>2</span> <span>from</span> Tree <span>inner</span> <span>join</span> <span>3</span> (<span>select</span> ID, Name,Parent,<span>cast</span>(Name <span>as</span> <span>nvarchar</span>(<span>max</span>)) <span>as</span> TE,<span>0</span> <span>as</span> Levle <span>from</span> Tree <span>where</span> Parent <span>is</span> <span>null</span><span>) </span><span>4</span> <span>as</span><span> CTE </span><span>5</span> <span>on</span> Tree.Parent<span>=</span>CTE.Name
上面的CTE子查询的结果就是第一次递归查询的结果集,上面SQL运行结果为:
同样的,将第二次递归查询的上面三条记录作为第三次查询的‘定位成员’:
【这里要注意,上面的三条记录是从最后一条开始依次作为第三次递归的输入的,即第一条是ID=9的记录,接下来是7和2,关于第四次递归也类似】
第三次递归类SQL语句
<span>1</span> <span>select</span> Tree.ID, Tree.Name,Tree.Parent,<span>cast</span>(<span>replicate</span>(<span>'</span> <span>'</span>,<span>len</span>(CTE.TE))<span>+</span><span>'</span><span>|_</span><span>'</span><span>+</span>Tree.name <span>as</span> <span>nvarchar</span>(<span>MAX</span>)) <span>as</span> TE,Levle<span>+</span><span>1</span> <span>as</span><span> Levle </span><span>2</span> <span>from</span> Tree <span>inner</span> <span>join</span> <span>3</span> (第二次递归查询的SQL语句)<span>as</span><span> CTE </span><span>4</span> <span>on</span> Tree.Parent<span>=</span>CTE.Name
结果如下:
其实每次递归的类SQL可为如下所示:
<span>1</span> <span>select</span> Tree.ID, Tree.Name,Tree.Parent,<span>cast</span>(<span>replicate</span>(<span>'</span> <span>'</span>,<span>len</span>(CTE.TE))<span>+</span><span>'</span><span>|_</span><span>'</span><span>+</span>Tree.name <span>as</span> <span>nvarchar</span>(<span>MAX</span>)) <span>as</span> TE,Levle<span>+</span><span>1</span> <span>as</span><span> Levle </span><span>2</span> <span>from</span> Tree <span>inner</span> <span>join</span> <span>3</span> <span>(上次递归查询的结果集,仅仅是上次那一次的,而不是以前的总和结果集) </span><span>4</span> <span>as</span><span> CTE </span><span>5</span> <span>on</span> Tree.Parent<span>=</span>CTE.Name
第四次递归一次类推,最后所查询的结果为上面所有递归的union。
续:在上面的SQ语句查询结果中,ID为10的记录应该要放在ID为4的后面。
往数据表中再次添加两条记录:
insert Tree values('计科','计算机学院') insert Tree values('我','网络1班')
再次修改上面的SQL语句:
<span> 1</span> <span>with</span> CTE <span>as</span> <span> 2</span> <span>( </span><span> 3</span> <span>--</span><span>>Begin 一个定位点成员 </span> <span> 4</span> <span>select</span> ID, Name,Parent,<span>cast</span>(Name <span>as</span> <span>nvarchar</span>(<span>max</span>)) <span>as</span><span> TE, </span><span> 5</span> ROW_NUMBER()<span>over</span>(<span>order</span> <span>by</span> <span>getdate</span>()) <span>as</span><span> OrderID </span><span> 6</span> <span>--</span><span>最关键是上面这个字段,要获取排序字段,按字符串来排序。</span> <span> 7</span> <span>--</span><span>其中窗口函数必须要使用order by,但是不能用整型,那就用时间吧</span> <span> 8</span> <span>from</span> Tree <span>where</span> Parent <span>is</span> <span>null</span> <span> 9</span> <span>--</span><span>>End </span> <span>10</span> <span>union</span> <span>all</span> <span>11</span> <span>--</span><span>>Begin一个递归成员 </span> <span>12</span> <span>select</span> Tree.ID, Tree.Name,Tree.Parent,<span>cast</span>(<span>replicate</span>(<span>'</span> <span>'</span>,<span>len</span>(CTE.TE))<span>+</span><span>'</span><span>|_</span><span>'</span><span>+</span>Tree.name <span>as</span> <span>nvarchar</span>(<span>MAX</span>)) <span>as</span><span> TE, </span><span>13</span> CTE.OrderID<span>*</span><span>100</span><span>+</span>ROW_NUMBER()<span>over</span>(<span>Order</span> <span>by</span> <span>GETDATE</span>()) <span>as</span><span> OrderID </span><span>14</span> <span>from</span> Tree <span>inner</span> <span>join</span><span> CTE </span><span>15</span> <span>on</span> Tree.Parent<span>=</span><span>CTE.Name </span><span>16</span> <span>--</span><span>>End </span> <span>17</span> <span>) </span><span>18</span> <span>select</span> <span>*</span> <span>from</span><span> CTE </span><span>19</span> <span>order</span> <span>by</span> <span>LTRIM</span>(OrderID)<span>--</span><span>最后将这个整型数据转换为字符串型的进行排序</span> <span>20</span> <span>21</span> <span>--</span><span>有时候整型可以比大小,字符串也可以,字符串比的大小是一位一位进行字符比较的</span> <span>22</span> <span>--</span><span>整型+字符串==整型,只有字符串+字符串==两个字符串的并和</span> <span>23</span> <span>--</span><span>递归查询中:第二条记录可以引用第一条记录的值</span> <span>24</span> <span>--</span><span>动态加载记录时,同一个等级的记录识别符:RowNumber()over(order by getdate())</span> <span>25</span> <span>--</span><span>延伸:可以动态获取某个部门下的所以子部门。也可以获取该部门上级的所以部门</span> <span>26</span> <span>27</span> <span>--</span><span>总结:首先要拼凑出一个整型数据,然后转换为字符串,最后是进行字符串的order,而不是整型数据的order,</span>
图2 运行结果
这样,无论用户插入多少条记录都可以进行按部门,按规律进行查询。

是的,C++Lambda表达式可以通过使用std::function支持递归:使用std::function捕获Lambda表达式的引用。通过捕获的引用,Lambda表达式可以递归调用自身。

给定两个字符串str_1和str_2。目标是使用递归过程计算字符串str1中子字符串str2的出现次数。递归函数是在其定义中调用自身的函数。如果str1是"Iknowthatyouknowthatiknow",str2是"know"出现次数为-3让我们通过示例来理解。例如输入str1="TPisTPareTPamTP",str2="TP";输出Countofoccurrencesofasubstringrecursi

我们以整数数组Arr[]作为输入。目标是使用递归方法在数组中找到最大和最小的元素。由于我们使用递归,我们将遍历整个数组,直到达到长度=1,然后返回A[0],这形成了基本情况。否则,将当前元素与当前最小或最大值进行比较,并通过递归更新其值以供后续元素使用。让我们看看这个的各种输入输出场景−输入 −Arr={12,67,99,76,32};输出 −数组中的最大值:99解释 &mi

Python是一门易学易用的编程语言,然而在使用Python编写递归函数时,可能会遇到递归深度过大的错误,这时就需要解决这个问题。本文将为您介绍如何解决Python的最大递归深度错误。1.了解递归深度递归深度是指递归函数嵌套的层数。在Python默认情况下,递归深度的限制是1000,如果递归的层数超过这个限制,系统就会报错。这种报错通常称为“最大递归深度错误

如何使用Vue表单处理实现表单的递归嵌套引言:随着前端数据处理和表单处理的复杂性不断增加,我们需要通过一种灵活的方式来处理复杂的表单。Vue作为一种流行的JavaScript框架,为我们提供了许多强大的工具和特性来处理表单的递归嵌套。本文将向大家介绍如何使用Vue来处理这种复杂的表单,并附上代码示例。一、表单的递归嵌套在某些场景下,我们可能需要处理递归嵌套的

在Linux系统中,“ls”命令是一个非常有用的工具,它提供了对当前目录中文件和文件夹的简洁概述。通过“ls”命令,您可以快速查看文件和文件夹的权限、属性等重要信息。虽然“ls”命令是一个基本的命令,但是通过结合不同的子命令和选项,它可以成为系统管理员和用户的重要工具。通过熟练使用“ls”命令及其各种选项,您可以更高效地管理文件系统,快速定位所需文件,以及执行各种操作。因此,“ls”命令不仅可以帮助您了解当前目录结构,还可以提高您的工作效率。比如,在Linux系统中,通过使用带有递归选项的"ls

注:本文以Go语言的角度来比较研究循环和递归。在编写程序时,经常会遇到需要对一系列数据或操作进行重复处理的情况。为了实现这一点,我们需要使用循环或递归。循环和递归都是常用的处理方式,但在实际应用中,它们各有优缺点,因此在选择使用哪种方法时需要考虑实际情况。本文将对Go语言中的循环和递归进行比较研究。一、循环循环是一种重复执行某段代码的机制。Go语言中主要有三

随着互联网的发展,各种网站和应用程序中都出现了树形结构的展示,例如分类目录、人员组织架构、权限管理等。在这些应用场景中,递归树结构已经成为了非常重要且实用的模型之一。ThinkPHP6是一种基于MVC模型的PHP开发框架,其拥有丰富的扩展库和优秀的性能,广受开发者的认可和使用,而在ThinkPHP6中实现递归树结构也变得更加方便了。下面,我们将介绍如何在Th


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver CS6
Visual web development tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
