sql >> Database >  >> RDS >> Mysql

somprijs van kinderen in andere tabel mysql

Probeer dit;)

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

Bewerkt

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



  1. meerdere toegevoegde entiteiten kunnen dezelfde primaire sleutel hebben op database seed

  2. MYSQL sum() voor verschillende rijen

  3. Probleem met tekencodering met linux en mysql

  4. PostgreSQL dumpen en herstellen in verschillende tijdzones