欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

MySql递归查询语句

程序员文章站 2022-05-12 18:52:01
...
MySql数据库,递归查询子节点

1.数据结构

  表名:tb_organization 字段名: id,parent_id,code,name

2.查询语句

SELECT  o2.* FROM (

SELECT @ids as _ids, ( SELECT @ids := GROUP_CONCAT(id) FROM tb_organization WHERE FIND_IN_SET(parent_id, @ids) ) as cids

FROM tb_organization, (SELECT @ids :='76350', @l := 0 ) b WHERE @ids IS NOT NULL

) id, tb_organization o2

WHERE FIND_IN_SET(o2.id, ID._ids)

ORDER BY  id


相关标签: Mysql 递归