select dept_id from (
select t1.dept_id,t1.parent_id,
if(find_in_set(parent_id, @pids) > 0, @pids := concat(@pids, ',', dept_id), 0) as ischild
from (
select dept_id,parent_id from sys_dept t where t.del_flag = '0' order by parent_id, dept_id
) t1,
(select @pids := 要查询的节点id) t2
) t3 where ischild != 0;

解释:

@pids := 要查询的节点id   变量定义。
concat()  拼接字符串
find_in_set(parent_id, @pids)  parent_id 字符串是否在@pids字符串中。
  select find_in_set('2','1,2');返回2
  select find_in_set('6','1');返回0 if(express1,express2,express3)条件语句,if语句类似三目运算符,当exprss1成立时,执行express2,否则执行express3;
拆分语句:select dept_id,parent_id from sys_dept t where t.del_flag = '0' order by parent_id, dept_id
select @pids := 要查询的节点id

select * from (
select dept_id,parent_id from sys_dept t where t.del_flag = '0' order by parent_id, dept_id
) t1,
(select @pids :=4) t2

select t1.dept_id,t1.parent_id,t2.*,
if(find_in_set(parent_id, @pids) > 0, @pids := concat(@pids, ',', dept_id), 0) as ischild
from (
select dept_id,parent_id from sys_dept t where t.del_flag = '0' order by parent_id, dept_id
) t1,
(select @pids := 4) t2

select dept_id from (
select t1.dept_id,t1.parent_id,t2.*,
if(find_in_set(parent_id, @pids) > 0, @pids := concat(@pids, ',', dept_id), 0) as ischild
from (
select dept_id,parent_id from sys_dept t where t.del_flag = '0' order by parent_id, dept_id
) t1,
(select @pids := 4) t2
) t3 where ischild != 0;

 

mysql 递归查找所有子节点的更多相关文章

  1. mysql 递归查找菜单节点的所有子节点

    背景                                                                                                   ...

  2. JavaScript之递归查找所有父节点

    ......data: () => ({ // 数据 dt: [{ id: '1', children: [ { id: '1-1', children: [ { id: '1-1-1', ch ...

  3. 公用表表达式 (CTE)、递归、所有子节点、sqlserver

    指定临时命名的结果集,这些结果集称为公用表表达式 (CTE).公用表表达式可以包括对自身的引用.这种表达式称为递归公用表表达式. 对于递归公用表达式来说,实现原理也是相同的,同样需要在语句中定义两部分 ...

  4. SQL 语句递归查询 With AS 查找所有子节点

    create table #EnterPrise (   Department nvarchar(50),--部门名称   ParentDept nvarchar(50),--上级部门   Depar ...

  5. MySql 利用函数 查询所有子节点

    前提:mysql  函数  find_in_set(str,strlist), cast(value as type)   一.find_in_set(str,strlist):如果字符串str是在的 ...

  6. dom4j递归解析XML字符串所有子节点

    /** * dom4j递归解析所有子节点 * * @param childElements * @param mapEle * @return */ public Map<String, Obj ...

  7. T-Sql 递归查询(给定节点查所有父节点、所有子节点的方法)

    -- 查找所有父节点with tab as( select Type_Id,ParentId,Type_Name from Sys_ParamType_V2_0 where Type_Id=316-- ...

  8. SQL 递归查询(根据指定的节点向上获取所有父节点,向下获取所有子节点)

    --------------------01.向上查找所有父节点-----------------WITH TEMP AS (SELECT * FROM CO_Department WHERE ID= ...

  9. [SQL]T-Sql 递归查询(给定节点查所有父节点、所有子节点的方法)

    T-Sql 递归查询(给定节点查所有父节点.所有子节点的方法)   -- 查找所有父节点with tab as( select Type_Id,ParentId,Type_Name from Sys_ ...

随机推荐

  1. 1.docker 慕课入门

    本文是学习慕课网的实战https://www.imooc.com/learn/824  同时结合菜鸟教程的思想https://www.runoob.com/docker/docker-architec ...

  2. RramSim2

    1.官网 http://www.computer.org/csdl/letters/ca/2011/01/lca2011010016-abs.html 2.介绍 http://blog.csdn.ne ...

  3. PTA(Basic Level)1006.Sign In and Sign Out

    At the beginning of every day, the first person who signs in the computer room will unlock the door, ...

  4. 常用的 Git 命令,给你准备好了!

    分支操作: git branch 创建分支 git branch -b 创建并切换到新建的分支上 git checkout 切换分支 git branch 查看分支列表 git branch -v 查 ...

  5. P4942小凯的数字

    给定一个序列,如12345 56789 1011121314等,输出对其取余9的结果. 那么我们需要明白一个定理,一个序列对一个数的取余结果等于它各位之和取余那个数的结果.证明似乎是这样∑i=0n​a ...

  6. Windows下使用SVN版本控制工具

    一.SVN工作原理 SVN(SubVersion)的基本工作思路是这样的:在一台服务器上建立一个源代码库,库里可以存放许多不同项目的源程序,由源代码库管理员统一管理这些源程序. 每个用户在使用源代码库 ...

  7. python_0基础开始_day09

    第九节 1,函数初始 s = "qwertyuiop"n = 0for i in s:    n += 1print(n)​lst = [1,2,3,4,5]n = 0for i ...

  8. Ubuntu14.04安装Caffe(CPU)

    一 安装Ubuntu14.04LTS Ubuntu分区 1.SWAP 交换分区:与物理内存相当. 2.“/” 根目录分区:该区大小由硬盘大小而定,10-100G. 3.“HOME” 家目录分区:该区也 ...

  9. Robot Framework(一)安装笔记

    参考网址:https://www.cnblogs.com/yinrw/p/5837828.html因为自己安装了py,网上教程都是统一安装py2.7开始的. 所以这里总结下安装笔记:cmd命令界面进行 ...

  10. GoAccess安装

    编译安装 yum install geoip-devel openssl-devel libmaxminddb-devel ncurses-devel bzip2-devel tokyocabinet ...