sum price of childs in other table mysql

后端 未结 2 624
谎友^
谎友^ 2021-01-15 17:57

I have two table one store data child and parent hierarchy and other paths and descendant

+----------+------------+-----------+
| userid   |    parent  |             


        
2条回答
  •  离开以前
    2021-01-15 18:12

    Try this;)

    select ancestor_id as userid, sum(b.price) as total
    from webineh_prefix_nodes_paths_tmp a 
    join webineh_prefix_nodes_tmp b 
    on b.userid = a.descendant_id
    where a.ancestor_id in (select userid from webineh_prefix_nodes_tmp where parent = 1)
    group by ancestor_id
    

    SQLFiddle Demo

    Edited

    select ancestor_id as userid, sum(b.price) as total
    from webineh_prefix_nodes_paths_tmp a 
    join webineh_prefix_nodes_tmp b 
    on b.userid = a.descendant_id
    inner join webineh_prefix_nodes_tmp c
    on a.ancestor_id = c.userid
    and c.parent = 1
    group by ancestor_id
    

    SQLFiddle Demo

提交回复
热议问题