-
2021-01-27 00:31:08
样例数据:
create table treeNodes(
id int primary key,
nodename varchar(20),
pid int
);
select * from treeNodes;
+----+----------+------+
| id | nodename | pid |
+----+----------+------+
| 1 | A | 0 |
| 2 | B | 1 |
| 3 | C | 1 |
| 4 | D | 2 |
| 5 | E | 2 |
| 6 | F | 3 |
| 7 | G | 6 |
| 8 | H | 0 |
| 9 | I | 8 |
| 10 | J | 8 |
| 11 | K | 8 |
| 12 | L | 9 |
| 13 | M | 9 |
| 14 | N | 12 |
| 15 | O | 12 |
| 16 | P | 15 |
| 17 | Q | 15 |
+----+----------+------+
17 rows in set (0.00 sec)
树形图如下
1:A
+-- 2:B
| +-- 4:D
| +-- 5:E
+-- 3:C
+-- 6:F
+-- 7:G
8:H
+-- 9:I
| +-- 12:L
| | +--14:N
| | +--15:O
| | +--16:P
| | +--17:Q
| +-- 13:M
+-- 10:J
+-- 11:K
创建一个function getChildLst, 得到一个由所有子节点号组成的字符串.
delimiter //
CREATE FUNCTION `getChildLst`(rootId INT)
RETURNS varchar(1000)
BEGIN
DECLARE sTemp VARCHAR(1000);
DECLARE sTempChd VARCHAR(1000);
SET sTemp = ‘$‘;
SET sTempChd =cast(rootId as CHAR);
WHILE sTempChd is not null DO
SET sTemp = concat(sTemp,‘,‘,sTempChd);
SELECT group_concat(id) INTO sTempChd FROM treeNodes where FIND_IN_SET(pid,sTempChd)>0;
END WHILE;
RETURN sTemp;
END
//
delimiter ;
使用我们直接利用find_in_set函数配合这个getChildlst来查找
select getChildLst(1);
+-----------------+
| getChildLst(1) |
+-----------------+
| $,1,2,3,4,5,6,7 |
+-----------------+
select * from treeNodes where FIND_IN_SET(id, getChildLst(1));
+----+----------+------+
| id | nodename | pid |
+----+----------+------+
| 1 | A | 0 |
| 2 | B | 1 |
| 3 | C | 1 |
| 4 | D | 2 |
| 5 | E | 2 |
| 6 | F | 3 |
| 7 | G | 6 |
+----+----------+------+
select * from treeNodes where FIND_IN_SET(id, getChildLst(3));
+----+----------+------+
| id | nodename | pid |
+----+----------+------+
| 3 | C | 1 |
| 6 | F | 3 |
| 7 | G | 6 |
+----+----------+------+
优点:简单,方便,没有递归调用层次深度的限制 (max_sp_recursion_depth,最大255);
缺点:长度受限,虽然可以扩大 RETURNS varchar(1000),但总是有最大限制的。
更多相关内容 -
MySQL递归减去和乘以分组的值
2021-07-16 22:50:50Unfortunately MYSQL doesn't support recursive queries, so you'll need to be a little creative here. Something like this could work: select flag, sum(calc) from ( select flag, (num-if(@prevflag=flag,@...Couldn't really explain my problem with words, but with an example I can show it clearly:
I have a table like this:
id num val flag
0 3 10 1
1 5 12 2
2 7 12 1
3 11 15 2
And I want to go through all the rows, and calculate the increase of the "num", and multiply that difference with the "val" value. And when I calculated all of these, I want to add these results together, but grouped based on the "flag" values.
This is the mathematical equation, that I want to run on the table:
Result_1 = (3-0)*10 + (7-3)*12
Result_2 = (5-0)*12 + (11-5)*15
78 = Result_1
150 = Result_2
Thank you.
解决方案
Interesting question. Unfortunately MYSQL doesn't support recursive queries, so you'll need to be a little creative here. Something like this could work:
select flag,
sum(calc)
from (
select flag,
(num-if(@prevflag=flag,@prevnum,0))*val calc,
@prevnum:=num prevnum,
@prevflag:=flag prevflag
from yourtable
join (select @prevnum := 0, @prevflag := 0) t
order by flag
) t
group by flag
-
mysql递归存储过程
2013-04-05 00:38:54包括两个存储过程,一个是建立临时表用来存储需要的数据,另一个利用临时表进行操作。 -
MySQL递归查询树状表的子节点、父节点具体实现
2020-09-10 16:09:16本程序写了两个sql存储过程,子节点查询算是照搬了,父节点查询是逆思维弄的 -
mysql 递归查询下级
2021-02-02 09:40:46CREATE TABLE `ys_recommendation_code_user` (`id` bigint NOT NULL AUTO_INCREMENT,`parent_user_id` int NOT NULL COMMENT '推荐者的用户id',`user_id` int NOT NULL COMMENT '用户id',`gmt_create` datetime NOT...CREATE TABLE `ys_recommendation_code_user` (
`id` bigint NOT NULL AUTO_INCREMENT,
`parent_user_id` int NOT NULL COMMENT '推荐者的用户id',
`user_id` int NOT NULL COMMENT '用户id',
`gmt_create` datetime NOT NULL,
`gmt_modified` datetime NOT NULL,
PRIMARY KEY (`id`) ,
INDEX `idx_parent_user_id_user_id` (`parent_user_id`, `user_id`)
)
COMMENT='推荐码-用户关联表';
2.插入数据
insert into `ys_recommendation_code_user` (`id`, `parent_user_id`, `user_id`, `gmt_create`, `gmt_modified`) values('2','100','102','0000-00-00 00:00:00','0000-00-00 00:00:00');
insert into `ys_recommendation_code_user` (`id`, `parent_user_id`, `user_id`, `gmt_create`, `gmt_modified`) values('3','100','103','0000-00-00 00:00:00','0000-00-00 00:00:00');
insert into `ys_recommendation_code_user` (`id`, `parent_user_id`, `user_id`, `gmt_create`, `gmt_modified`) values('4','101','104','0000-00-00 00:00:00','0000-00-00 00:00:00');
insert into `ys_recommendation_code_user` (`id`, `parent_user_id`, `user_id`, `gmt_create`, `gmt_modified`) values('5','101','105','0000-00-00 00:00:00','0000-00-00 00:00:00');
insert into `ys_recommendation_code_user` (`id`, `parent_user_id`, `user_id`, `gmt_create`, `gmt_modified`) values('6','101','106','0000-00-00 00:00:00','0000-00-00 00:00:00');
insert into `ys_recommendation_code_user` (`id`, `parent_user_id`, `user_id`, `gmt_create`, `gmt_modified`) values('7','104','107','0000-00-00 00:00:00','0000-00-00 00:00:00');
insert into `ys_recommendation_code_user` (`id`, `parent_user_id`, `user_id`, `gmt_create`, `gmt_modified`) values('8','104','108','0000-00-00 00:00:00','0000-00-00 00:00:00');
insert into `ys_recommendation_code_user` (`id`, `parent_user_id`, `user_id`, `gmt_create`, `gmt_modified`) values('9','104','109','0000-00-00 00:00:00','0000-00-00 00:00:00');
3.查询
SELECT user_id FROM (
SELECT t1.user_id,
IF(FIND_IN_SET(parent_user_id, @pids) > 0, @pids := CONCAT(@pids, ',', user_id), 0) AS ischild
FROM (
SELECT user_id,parent_user_id FROM ys_recommendation_code_user t ORDER BY parent_user_id, user_id
) t1,
(SELECT @pids := 这里是需要查询的user_id) t2
) t3 WHERE ischild != 0
4.查询结果 user_id = 100
-
mysql 递归算法,统计数量
2016-04-14 09:00:24CREATE TABLE Org( org_code varchar(80) PRIMARY ...要求输入就够编码(org_code),统计该机构下所有的项目数量(不含本机构) 例如输入24500000000 在统计结果如下 西安市税务局 4 咸阳市税务局 2 宝鸡市税务局 4 -
MySQL 递归查询 —— 树形数据
2020-04-18 18:17:23文章目录1、创建表:2、数据:3、递归的函数:4、查询4.1、 查询14.2、 查询14.3、 查询24.4、 查询34.5、 查询4 1、创建表: create table `tree` ( `id` int(11) not null auto_increment comment '主键', `...1、创建表:
create table `tree` ( `id` int(11) not null auto_increment comment '主键', `parent_id` int(11) not null, `name` varchar(10) not null, primary key (`id`) ) engine=innodb default charset=utf8;
2、数据:
insert into `tree` (`id`, `parent_id`, `name`) values('1','0','根节点'); insert into `tree` (`id`, `parent_id`, `name`) values('2','1','一级节点a'); insert into `tree` (`id`, `parent_id`, `name`) values('3','1','一级节点b'); insert into `tree` (`id`, `parent_id`, `name`) values('4','1','一级节点c'); insert into `tree` (`id`, `parent_id`, `name`) values('5','3','二级节点aa'); insert into `tree` (`id`, `parent_id`, `name`) values('6','4','二级节点bb'); insert into `tree` (`id`, `parent_id`, `name`) values('7','2','二级节点cc'); insert into `tree` (`id`, `parent_id`, `name`) values('8','5','三级节点aaa'); insert into `tree` (`id`, `parent_id`, `name`) values('9','5','三级节点bbb'); insert into `tree` (`id`, `parent_id`, `name`) values('10','5','三级节点ccc'); insert into `tree` (`id`, `parent_id`, `name`) values('11','6','三级节点ddd'); insert into `tree` (`id`, `parent_id`, `name`) values('12','11','四级节点aaaa'); insert into `tree` (`id`, `parent_id`, `name`) values('13','10','四级节点bbbb');
3、递归的函数:
drop function if exists getChildList; delimiter // create function `getChildList`(rootId int) returns varchar(1000) begin declare result varchar(1000); declare tempChild varchar(1000); set result = ''; set tempChild =cast(rootId as char); while tempChild is not null do set result = concat(result,',',tempChild); select group_concat(id) into tempChild from tree where find_in_set(parent_id,tempChild)>0; end while; return result; end // delimiter ;
4、查询
tree 表所有的数据,如下:
4.1、 查询1
select getChildList(1);
结果:
,1,2,3,4,5,6,7,8,9,10,11,12,13
4.2、 查询1
select getChildList(2);
结果:
,2,7
4.3、 查询2
select * from tree where find_in_set(id, getChildList(2));
4.4、 查询3
select * from tree where find_in_set(id, getChildList(3));
4.5、 查询4
select * from tree where find_in_set(id, getChildList(4));
-
mysql 递归查找无限极分类的某一节点的所有子节点
2021-02-02 12:39:36但是当这个无限极分类数据量特别大的情况下,比如有几万行的数据的情况下,这种做法似乎也不是最好的,这里分享一种通过mysql语句查找出任意节点的所有子节点的方法。表结构如下:CREATE TABLE `menu` ... -
mysql递归查子部门
2020-05-23 18:27:55DROP FUNCTION IF EXISTS queryChildrenStaff; DELIMITER ;; CREATE FUNCTION queryChildrenStaff(StaffId INT) RETURNS VARCHAR(4000) BEGIN DECLARE sTemp VARCHAR(4000); DECLARE sTempChd VARCHAR(4000);... -
mysql 递归查找部门下面的所有被这个部门管理的部门(mysql 递归查找节点的所有子节点)
2019-03-15 11:37:15今天小编遇到了一个需求,需要使用mysql查询部门下面所有子部门,换句...用来递归查询子节点,但是MySQL不存在这个,所以在网上就去百度了一下,发现很多代替oracle中的start with .... connect by pripor。但是看着... -
mysql如何实现递归查询
2021-02-11 00:09:08mysql实现递归查询的方法:首先创建表,并初始化数据;然后向下递归,利用【find_in_set()】函数和【group_concat()】函数实现递归查询。 本教程操作环境:windows7系统、mysql8.0.22版,该方法适用于所有品牌电脑。... -
mysql -- 递归查询所有子节点
2020-08-07 13:49:59mysql 实现完整语句 根据以上思路,我们可以通过以下mysql函数,完成递归查询。 RETURNS varchar(1000) READS SQL DATA BEGIN DECLARE sTemp VARCHAR(1000); DECLARE sTempChd VARCHAR(1000); SET sTemp = ‘$’; ... -
mysql递归查询所有下属员工函数以及find_in_set函数
2018-09-04 13:59:00今天做一个页面查询时有个需求是:不同人查看当前页面的数据量不一样,...因此写了个传入父id查询所有子id的mysql函数,同样适用于,菜单查询,组织机构查询等等上下级关系的递归查询。 DELIMITER $$ USE `loan... -
MySQL递归查询所有下级节点
2021-07-12 10:38:15MySQL递归查询所有下级节点。 先上SQL后来讲解其中的含义。 select id from ( select t1.id,t1.inviteId, if(find_in_set(inviteId, @pids) > 0, @pids := concat(@pids, ',', id), 0) as isbig from ( ... -
MYSQL递归查询所有父节点
2021-01-11 19:04:171、表结构:CREATE TABLE `t_busi_system` (`ID` varchar(64) NOT NULL COMMENT '标识',`PARENT_ID` varchar(64) DEFAULT NULL COMMENT '父id',`CREATE_DATE` varchar(64) DEFAULT NULL COMMENT '创建时间',`CREATE_... -
mysql统计子节点数量函数
2021-02-18 16:54:17DELIMITER $$ USE `rcj`$$ DROP FUNCTION IF EXISTS `getInviteMemberList`$$ CREATE DEFINER=`root`@`localhost` FUNCTION `getInviteMemberList`(fromInviteCode VARCHAR(1000)) RETURNS INT ... -
mysql递归调用获取树节点(子树)
2010-09-13 08:45:34mysql递归调用获取树节点(子树),使用存储过程实现子树的节点的查询,内附有word文件完整说明,和测试数据表的脚本文件。 -
MySql递归查询父级/子级数据
2020-07-01 09:29:381.根据父级ID递归查询所有下级ID SELECT id FROM ( SELECT t1.id, IF ( FIND_IN_SET( t1.parent_id, @pids ) > 0, @pids := CONCAT( @pids, ',', t1.id ), 0 ) AS ischild FROM ( ... -
实现MySQL递归查询树状表的子节点、父节点具体方法
2020-12-18 17:56:20MySQL递归查询树状表的子节点、父节点具体实现简介:mysql5.0.94版本,该版本以及较高级的版本(5.5、6等等)尚未支持循环递归查询,和sqlserver、oracle相比,mysql难于在树状表中层层遍历的子节点。本程序重点参考了... -
MySQL高效查询某节点下的所有子节点
2021-01-19 07:00:10MySQL不是T-SQL,没有cte,那么要查询子节点就会比较麻烦,需要在程序中使用到递归查询。比较好的做法是,在建表的时候,就要考虑到此问题,需要有一个存节点层级信息的字段,比如这样:设置方法为:public int ... -
mysql实现多表关联统计(子查询统计)示例
2020-12-15 23:48:08本文实例讲述了mysql实现多表关联统计的方法。分享给大家供大家参考,具体如下: 需求: 统计每本书打赏金额,不同时间的充值数据统计,消费统计, 设计四个表,book 书本表,orders 订单表 reward_log打赏表 ... -
mysql 递归查询树形目录
2021-03-03 21:15:29早些时候看到一篇《一句SQL实现MYSQL的递归查询》,但是必须是在 id大于pid 的情况下才能使用。创建表格CREATE TABLE `treenodes` (`id` int , -- 节点ID`nodename` varchar (60), -- 节点名称`pid` int -- 节点父ID... -
Mysql中的递归层次查询(父节点下的所有节点)
2021-02-05 20:16:06在mysql中如何完成节点下的所有节点或节点上的所有父节点的查询? 在Oracle中我们知道有一个Hierarchical Queries可以通过CONNECT BY来查询,但是,在MySQL中还没有对应的函数!!! 下面给出一个function来完成的方法 ... -
python3.6.3递归爬取2018年最新国家统计局地区数据存入mysql
2019-06-04 16:45:17python3.6.3递归爬取国家统计局地区数据存入mysql,可以自定义爬取省市区县乡镇社区5级 数据来源:http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2018/index.html get_level爬取到哪一级的数据 1省,2市,3区县... -
mysql 递归查找菜单节点的所有子节点
2021-05-18 14:08:31出处: ...背景 项目中遇到一个需求,...因此在这里采用类似递归的方法对菜单的所有子节点进行查询。 准备 创建menu表: CREATE TABLE menu ( id int(11) NOT NULL AUTO_INCREMENT COMMENT ‘菜单id’, parent_id int(11 -
mysql 自定义函数 递归查找父节点
2021-03-10 17:41:071.问题描述: 现有一树形机构数据表,只有子父关系...写一个递归查询父节点的函数 ①创建函数向导 ②函数内容 编写完成点保存,然后就可以用了 CREATE DEFINER=`root`@`%` FUNCTION `getParentNodeList`.. -
MySQL递归查询,实现上下级联查,父子级查询
2019-04-11 17:33:25抓取国家统计局区划数据 可以测试数据: INSERT INTO `tb_area` VALUES ('1', '中国', '100000', '0'); INSERT INTO `tb_area` VALUES ('2', '广西壮族自治区', '450000000000', '1'); INSERT... -
MySQL中树形递归检索_MySQL
2021-03-03 21:15:41来进行递归检索查询。在MySQL中暂无相关函数,可以通过自定义函数方式来解决;函数创建:进入MySQL Command Line Client mysql> delimiter $$mysql>mysql> CREATE FUNCTION `getTreeNodes`(rootId INT...