我有2个表:table1,table2
table1
table2
Parent Child Point Parent Total a b 100 a 0(default) (result = 1050) b c 200 b 0 (result = 950) c d 250 c 0 (result = 750) d e 500
的结果table2应为基于中的父项的子项总和table1。
a---b---c---d---e
我尝试了很多次,但无法弄清楚。
UPDATE table2 set Total=???
使用递归CTE:
WITH RECURSIVE cte AS ( SELECT parent, child, point AS total FROM tbl1 UNION ALL SELECT c.parent, t.child, c.total + t.point FROM cte c JOIN tbl1 t ON t.parent = c.child ) SELECT * FROM cte