[Solved] SQL SUM COLUMN FROM A SAME TABLE AND Multi ids


You can use a recursive CTE to get all the linked users:

with recursive u as
(select t.id, t.`Referral id`, t.Balance from yourtable t where id = 1
union
select t.id, t.`Referral id`, t.Balance from u inner join yourtable t
on u.id = t.`Referral id`)
(select sum(Balance) from u)

Fiddle

0

solved SQL SUM COLUMN FROM A SAME TABLE AND Multi ids