Sql语句之递归查询

直接进入正题

比如一个表,有id和pId字段,id是主键,pid表示它的上级节点,表结构和数据:

 

CREATE TABLE [aaa](
 [id] [int] NULL,
 [pid] [int] NULL,
 [name] [nchar](10)
)
GO
INSERT INTO aaa VALUES(1,0,'a')
INSERT INTO aaa VALUES(2,0,'b')
INSERT INTO aaa VALUES(3,1,'c')
INSERT INTO aaa VALUES(4,1,'d')
INSERT INTO aaa VALUES(5,2,'e')
INSERT INTO aaa VALUES(6,3,'f')
INSERT INTO aaa VALUES(7,3,'g')
INSERT INTO aaa VALUES(8,4,'h')
GO
--下面的Sql是查询出1结点的所有子结点
with my1 as(select * from aaa where id = 1
 union all select aaa.* from my1, aaa where my1.id = aaa.pid
)
select * from my1 --结果包含1这条记录,如果不想包含,可以在最后加上:where id <> 1
--下面的Sql是查询出8结点的所有父结点
with my1 as(select * from aaa where id = 8
 union all select aaa.* from my1, aaa where my1.pid = aaa.id
)
select * from my1;
 --下面是递归删除1结点和所有子结点的语句:
with my1 as(select * from aaa where id = 1
   union all select aaa.* from my1, aaa where my1.id = aaa.pid
)
delete from aaa where exists (select id from my1 where my1.id = aaa.id)

二:

SQLserver2008使用表达式递归查询

--由父项递归下级
with cte(id,parentid,text)
as
(--父项
select id,parentid,text from treeview where parentid = 450
union all
--递归结果集中的下级
select t.id,t.parentid,t.text from treeview as t
inner join cte as c on t.parentid = c.id
)
select id,parentid,text from cte
--由子级递归父项
with cte(id,parentid,text)
as
(--下级父项
select id,parentid,text from treeview where id = 450
union all
--递归结果集中的父项
select t.id,t.parentid,t.text from treeview as t
inner join cte as c on t.id = c.parentid
)
select id,parentid,text from cte

 

 

转载于:https://www.cnblogs.com/ckblogs/p/3679787.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/423831.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

mybatis学习(20):模糊查询$

目录结构 com.geyao.mybatis.mapper BlogMapper类 package com.geyao.mybatis.mapper;import java.util.List;import com.geyao.mybatis.pojo.Blog;public interface BlogMapper {Blog selectBlog(Integer id);Blog selectBlog2(Integer id);List<Blog> selectBlogByTi…

WX: picker 滚动选择器

http://www.wxappclub.com/doc/1-13 转载于:https://www.cnblogs.com/Aozorazy/p/11275117.html

Python标准异常总结

AssertionError 断言语句&#xff08;assert&#xff09;失败 AttributeError 尝试访问未知的对象属性 EOFError 用户输入文件末尾标志EOF&#xff08;Ctrld&#xff09; FloatingPointError 浮点计算错误 GeneratorExit generator.close()方法被调用的时候 ImportError 导入模…

mybatis学习(21):MySQL 字符串 转换 CAST与CONVERT 函数的用法

MySQL 的CAST()和CONVERT()函数可用来获取一个类型的值&#xff0c;并产生另一个类型的值。两者具体的语法如下&#xff1a; Sql代码 CAST(value as type); CONVERT(value, type); 就是CAST(xxx AS 类型), CONVERT(xxx,类型)。 Sql代码 mysql> SELECT CAST(3.35 …

VUE初始化一个项目

https://www.cnblogs.com/huihuijiang/p/8252851.html ESLint 如果选了yes&#xff0c;会以严格模式规范你的代码&#xff0c;如果你想规范化开发可以选择yes,如果觉得影响开发进度就可以选no 转载于:https://www.cnblogs.com/kstudy/p/11278122.html

【转载】快速、可伸缩和流式的AJAX代理--跨域持续内容分发

原文&#xff1a;《Fast, Scalable, Streaming AJAX Proxy - continuously deliver data from across domains》 作者&#xff1a;Omar Al ZabirURL&#xff1a; http://www.codeproject.com/KB/ajax/ajaxproxy.aspx Download source - 16.1 KB Introduction Due to browsers p…

pycharm常用设置(keymap设置及eclipse常用快捷键总结)

2015-04-15 13:23 23800人阅读 评论(0) 收藏 举报分类&#xff1a;openstack-环境及安装配置&#xff08;10&#xff09; 版权声明&#xff1a;欢迎大家转载&#xff0c;转载请注明出处blog.csdn.net/tantexian。 设置pycharm为Eclipse快捷键后使用总结&#xff1a; Ctrl O 根…

mybatis学习(22):查询排序

按照某列排序 目录结构 com.geyao.mybatis.mapper BlogMapper类 package com.geyao.mybatis.mapper;import java.util.List;import com.geyao.mybatis.pojo.Blog;public interface BlogMapper {Blog selectBlog(Integer id);Blog selectBlog2(Integer id);List<Blog> …

MSSQL 2005 分页分析及优化(转)

MSSQL 2005 分页分析及优化 MSSQL 分页方式说明:目前我所知的有以下几种方式临时表 表变量 in, not in SET ROWCOUNT CTE id >, id < 优缺点分析: 性能最低, 可操作性差第一种方式和第二种方实际上是比较类似的.优点: 排序方式比较随意缺点:第一种方式 有大量的 IO 开销…

python基础:python循环、三元运算、字典、文件操作

目录&#xff1a; python循环三元运算字符串字典文件操作基础 一、python编程 在面向过程式编程语言的执行流程中包含&#xff1a; 顺序执行 选择执行 循环执行 if是条件判断语句&#xff1b;if的执行流程属于选择执行&#xff1b;if语句有三种格式&#xff0c;如下&#xff1…

mybatis学习(23):分页1 多参数传递(索引方式)

分页排序 目录结构 com.geyao.mybatis.mapper BlogMapper类 package com.geyao.mybatis.mapper;import java.util.List;import com.geyao.mybatis.pojo.Blog;public interface BlogMapper {Blog selectBlog(Integer id);Blog selectBlog2(Integer id);List<Blog> sele…

1.怎样定制VC#DataGrid列标题?

1.怎样定制VC#DataGrid列标题&#xff1f;DataGridTableStyle dgts new DataGridTableStyle(); dgts.MappingName "myTable"; //myTable为要载入数据的DataTableDataGridTextBoxColumn dgcs new DataGridTextBoxColumn(); dgcs.MappingName "title_id"…

[优先队列][堆] Luogu P4505 组合子逻辑

题目描述 组合子逻辑是 Moses Schnfinkel 和 Haskell Curry 发明的一种符号系统&#xff0c;用于消除数理逻辑中对于变量的需要。本题考察一种与真实世界的组合子演算略有差别的组合子系统。 一个组合子项是下列形式之一&#xff1a; PP (E_1\;E_2)(E1​E2​) 其中 PP 表示一个…

mybatis学习(24):分页2 多参数传递(使用注解)

分页排序 目录结构 com.geyao.mybatis.mapper BlogMapper类 package com.geyao.mybatis.mapper;import java.util.List;import org.apache.ibatis.annotations.Param;import com.geyao.mybatis.pojo.Blog;public interface BlogMapper {Blog selectBlog(Integer id);Blog se…

Python -- 三元表达式(三目运算符)

一些语言&#xff08;如Java&#xff09;的三元表达式形如&#xff1a; [plain] view plaincopy 判定条件?为真时的结果:为假时的结果 Python的三元表达式有如下几种书写方法&#xff1a; [python] view plaincopy if __name__ __main__: a b True c …

网页广告代码

http://blog.tangcs.com/2009/03/03/web-page-ad-code/转载于:https://www.cnblogs.com/WarrenTang/archive/2009/03/03/1402521.html

Vista SP1、IIS7,安装ASP.Net 1.1、VS2003、NetAdvantage 2004vol、Sql Server2000全攻略

对于微软的软件&#xff0c;一向认为是&#xff1a;beta版惨不忍睹&#xff0c;rtm版马马虎虎&#xff0c;sp1版。所以&#xff0c;现在Vista SP1出来了&#xff0c;我也考虑更换操作系统了。经过一番google&#xff0c;唯一的问题应该就在于ASP.Net 1.1在Vista上的运行调试了&…

mybatis学习(25):分页3 多参数传递(使用map)

分页排序 目录结构 com.geyao.mybatis.mapper BlogMapper类 package com.geyao.mybatis.mapper;import java.util.List; import java.util.Map;import org.apache.ibatis.annotations.Param;import com.geyao.mybatis.pojo.Blog;public interface BlogMapper {Blog selectBl…

2019CCPC湖南全国邀请赛-Chika and Friendly Pairs- 莫队+树状数组+离散化

题目链接&#xff1a; http://acm.hdu.edu.cn/showproblem.php?pid6534 思路&#xff1a; 莫队算法&#xff0c;树状数组求和。每个数对应的离散化之后的值需要打表存下来。 Code&#xff1a; 1 #include<bits/stdc.h>2 using namespace std;3 const int N2700010;4 int…

用 GDI 操作 EMF 文件[6]: GetEnhMetaFileHeader - 获取 EMF 的头文件

//增强图元文件的头文件结构 TEnhMetaHeader: tagENHMETAHEADER packed recordiType: DWORD; {记录类型}nSize: DWORD; {结构大小}rclBounds: TRect; {外接矩形(单位是像素)}rclFrame: TRect; {图片矩形(单位是 0.1 毫米)}dSignature: DWORD; …