sum price of childs in other table mysql

后端 未结 2 625
谎友^
谎友^ 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

    0 讨论(0)
  • 2021-01-15 18:33

    try this

    select sum(b.price) from webineh_prefix_nodes_paths_tmp a join webineh_prefix_nodes_tmp b on (b.userid = a.descendant_id) where a.ancestor_id in ( 1,2,3) GROUP by ancestor_id
    
    0 讨论(0)
提交回复
热议问题