一条SQL 可以解决吗。可以请直接上SQL
1条回答 默认 最新
关注 --测试数据 create table test_20220217 (id int,name VARCHAR(2),p_id int,money int); insert into test_20220217 values(1,'A',-1,10); insert into test_20220217 values(2,'B',1,15); insert into test_20220217 values(3,'C',1,12); insert into test_20220217 values(4,'D',2,11); insert into test_20220217 values(5,'E',2,13); insert into test_20220217 values(6,'F',3,14); insert into test_20220217 values(7,'G',4,20); insert into test_20220217 values(8,'H',4,31); insert into test_20220217 values(9,'I',5,55); --查询 WITH RECURSIVE cte as (select t.*,1 lvl,cast(null as int) third_node from test_20220217 t where id=1 union all select t.*,lvl+1,case when lvl+1=3 then t.id when lvl+1>3 then cte.id end from test_20220217 t,cte where cte.id=t.p_id ) select third_node,sum(money) from cte where third_node is not null group by third_node
查询效果
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用
悬赏问题
- ¥15 linux驱动,linux应用,多线程
- ¥20 我要一个分身加定位两个功能的安卓app
- ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
- ¥15 IAR程序莫名变量多重定义
- ¥15 (标签-UDP|关键词-client)
- ¥15 关于库卡officelite无法与虚拟机通讯的问题
- ¥15 目标检测项目无法读取视频
- ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
- ¥100 求采集电商背景音乐的方法
- ¥15 数学建模竞赛求指导帮助