原文链接地址http://www.cnblogs.com/sweting/archive/2009/06/08/1498483.html

在工作中遇到一个问题,是需要sql递归查询的.不懂,于是到csdn上去问,那里的效率真是非常高,我以前也没在上面问过问题.
 
问题描述:
 
我有一个表结构如下:
id upperid
1     2
3     2
4     1
5     3
具体层次不知道,我想用递归sql语句把所有属于某个upperid的数据,包括它的子树,都读出去,请问应该子怎么写?      
比如说 upperid =2
那么先找到1,3,然后再由1,3找到4,5
使用sql语句实现
 
有两位朋友都给了回复:
fa_ge(鶴嘯九天)
Create table t(id int,upperid int)
insert into t
select 1,     2
union all select 3,     2
union all select 4,     1
union all select 5,     3
select * from t
create function aa(@upperid int)
returns @t table (id int,upperid int,level int)
as
begin
declare @i int
set @i=1
insert into @t
select *,@i from t where upperid=@upperid
while @@rowcount>0
begin
set @i=@i+1
insert into @t
select a.*,@i from t a left join @t b on a.upperid=b.id
where b.level=@i-1
end
return
end
select * from dbo.aa(1)
id          upperid     level       
----------- ----------- ----------- 
4           1           1
(所影响的行数为 1 行)
select * from dbo.aa(2)
id          upperid     level       
----------- ----------- ----------- 
1           2           1
3           2           1
4           1           2
5           3           2
(所影响的行数为 4 行)
这个需要level这个数,否则得不到.
 
hellowork(一两清风)
----创建测试数据
if object_id('tbTest') is not null
drop table tbTest
if object_id('spGetChildren') is not null
drop proc spGetChildren
GO
create table tbTest(id int, upperid int)
insert tbTest
select 1,     2 union all
select 3,     2 union all
select 4,     1 union all
select 5,     3
GO
----创建存储过程
create proc spGetChildren @id int
as
    declare @t table(id int)
    insert @t select id from tbTest where upperid = @id
    while @@rowcount > 0
        insert @t select a.id from tbTest as a inner join @t as b
        on a.upperid = b.id and a.id not in(select id from @t)
    select * from @t
GO
----执行存储过程
declare @upperid int
set @upperid = 2
EXEC spGetChildren @upperid
----清除测试环境
drop proc spGetChildren
drop table tbTest
/*结果
id          
----------- 
1
3
4
5
*/
这个就符合我的要求了.
 
不过我需要的是一个函数,于是改写如下;
create function GetChildren (@id varchar(20))
returns @t table(id varchar(20))
as
begin
    insert @t select wayid from tb where upperwayid = @id
    while @@rowcount > 0
        insert @t select a.wayid from tb as a inner join @t as b
        on a.upperwayid = b.id and a.wayid not in(select id from @t)
   return
end
 
哈哈,真是爽啊.
 
csdn问题地址:
http://community.csdn.net/Expert/topic/5731/5731880.xml?temp=.8160211
 
原来为了解决这个问题,本来想用递归的,在网上看到了下面的资料:

表结构是这样的

部门    上级部门    
A           B
B           C
C           D
A           A
B           B
C           C

求一条SQL语句,根据A查其上级部门,查询结果为
上级部门
B
C
D

=================================================

用函数
create table tb (部门 varchar(20),上级部门 varchar(20))

insert into tb select 'A','B' union all select 'B','C' union all select 'C','D' 
union all select 'A','A' union all select 'B','B' union all select 'C','C'

--select * from tb
create function test_f (@name varchar(20))
returns @ta table(上级部门 varchar(20))
as 
begin 
--select @name=上级部门 from tb where 部门=@name and 部门!=上级部门
while exists(select 1 from tb where 部门=@name and 部门!=上级部门)
begin
insert @ta select 上级部门 from tb where 部门=@name and 部门!=上级部门
select @name=上级部门 from tb where 部门=@name and 部门!=上级部门
end
return
end

select * from dbo.test_f('A')

删除:
drop function test_f
drop table tb

上级部门                 
-------------------- 
B
C
D

(所影响的行数为 3 行)

(转自:http://blog.csdn.net/jackeyabc/archive/2007/03/19/1533775.aspx)

但是可以从部门到上级部门,却不知道怎么修改成为从上级部门到部门.所以最终没有采用

【转】sql递归查询问题的更多相关文章

  1. SQL递归查询实现跟帖盖楼效果

    网易新闻的盖楼乐趣多,某一天也想实现诸如网易新闻跟帖盖楼的功能,无奈技术不佳(基础不牢),网上搜索了资料才发现SQL查询方法有一种叫递归查询,整理如下: 一.查询出 id = 1 的所有子结点 wit ...

  2. SQL递归查询(with as)

    SQL递归查询(with cte as) with cte as(    select Id,Pid,DeptName,0 as lvl from Department    where Id = 2 ...

  3. 关于SQL递归查询在不同数据库中的实现方法

    比如表结构数据如下: Table:Tree ID Name ParentId 1 一级  0 2  二级 1 3  三级 2 4 四级 3 SQL SERVER 2005查询方法: //上查 with ...

  4. SQL递归查询知多少

    最近工作中遇到了一个问题,需要根据保存的流程数据,构建流程图.数据库中保持的流程数据是树形结构的,表结构及数据如下图: 仔细观察表结构,会发现其树形结构的特点: FFIRSTNODE:标记是否为根节点 ...

  5. sql递归查询语句

    sql Bom 递归查询: with t as(select * from Department where id=6union allselect a.* from Department a,t w ...

  6. SQL递归查询实现组织机构树

    系统用到的组织机构树,要实现对当前节点以及其子节点的查询,数据库SQL要用到递归查询,这也是我第一次接触SQL的递归查询. 先说一下什么是递归查询,简单说来是将一个树状结构存储在一张表里,比如一个表中 ...

  7. SQL 递归查询,意淫CTE递归的执行步骤

    今天用到了sql的递归查询.递归查询是CTE语句with xx as(....)实现的. 假如表Category数据如下. 我们想查找机枪这个子分类极其层次关系(通过子节点,查询所有层级节点).以下是 ...

  8. easyUI 的tree 修改节点,sql递归查询

    1.easyUI 的tree 修改节点: 我需要:切换语言状态,英文下, 修改根节点文本,显示英文. 操作位置:在tree的显示 $('#tree').tree(),onLoadSuccess事件方法 ...

  9. SQL递归查询(with cte as)

    with cte as ( select Id,Pid,DeptName,0 as lvl from Department where Id = 2 union all select d.Id,d.P ...

随机推荐

  1. Linux驱动之LED驱动编写

    从上到下,一个软件系统可以分为:应用程序.操作系统(内核).驱动程序.结构图如下:我们需要做的就是写出open.read.write等驱动层的函数.一个LED驱动的步骤如下: 1.查看原理图,确定需要 ...

  2. 201621123008 《Java程序设计》 第11周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多线程相关内容. 2. 书面作业 本次PTA作业题集多线程 1. 源代码阅读:多线程程序BounceThread 1.1 BallR ...

  3. Codeforces 757B. Bash's Big Day GCD

    B. Bash's Big Day time limit per test:2 seconds memory limit per test:512 megabytes input:standard i ...

  4. mysql 设置用户并授权

    一, 创建用户: 命令:CREATE USER 'username'@'host' IDENTIFIED BY 'password'; 说明:username - 你将创建的用户名, host - 指 ...

  5. 《C#从现象到本质》读书笔记(二)第2章 C#类型基础(上)

    <C#从现象到本质>读书笔记第二篇 第2章 C#类型基础(上) 类型指的是集合{类,结构,接口,枚举,委托}中的任意一个成员.任何拥有某类型的值(value)称为某类型的一个实例(inst ...

  6. 2017/2/14:JSTL标签与el表达式

    一.JSTL标签介绍 1.什么是JSTL? JSTL是apache对EL表达式的扩展(也就是说JSTL依赖EL),JSTL是标签语言!JSTL标签使用以来非常方便,它与JSP动作标签一样,只不过它不是 ...

  7. 【Redis】使用Jedis操作Redis

    Jedis介绍 jedis就是集成了redis的一些命令操作,封装了redis的java客户端. Jedis使用 使用jedis需要引入jedis的jar包,下面提供了maven依赖 jedis.ja ...

  8. hdu 6208(后缀自动机、或者AC自动机

    题意:给你n个字符串,问你是否存在一个字符串可以从中找到其他n-1个字符串. 思路:其实很简单,找到最长的那个字符串对他进行匹配,看是否能匹配到n-1个字符串. 可以用AC自动机或者后缀自动机做,但是 ...

  9. 用sql 生成2016年全年的日期

    select to_char(日期,'yyyy-mm-dd') from( select to_date('2016-01-01','yyyy-mm-dd') + level 日期 from dual ...

  10. 2019.01.23 hdu1693 Eat the Trees(轮廓线dp)

    传送门 题意简述:给一个有障碍的网格图,问用若干个不相交的回路覆盖所有非障碍格子的方案数. 思路:轮廓线dpdpdp的模板题. 同样是讨论插头的情况,只不过没有前一道题复杂,不懂的看代码吧. 代码: ...