I have the following table:
id | parent_id | quantity
-------------------------
1 | null | 5
2 | null | 3
3 | 2 | 10
4 | 2 | 15
How about avoiding procedures:
SELECT quantity from (
SELECT @rq:=parent_id as id, @val:=@val*quantity as quantity from (
select * from testTable order by -id limit 1000000 # 'limit' is required for MariaDB if we want to sort rows in subquery
) t # we have to inverse ids first in order to get this working...
join
( select @rq:= 6 /* example query */, @val:= 1 /* we are going to multiply values */) tmp
where id=@rq
) c where id is null;
Check out Fiddle!
Note! this will not work if row's parent_id>id.
Cheers!