sql >> Databasteknik >  >> RDS >> Mysql

summa pris av barn i annan tabell mysql

Prova detta;)

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

Redigerad

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. Hur kontrollerar jag SQL-replikeringsstatus via T-SQL?

  2. ORA-00979:inte ett GROUP BY-uttryck när jag kör min sql

  3. SQL - räkna in i flera kolumner

  4. Fel vid körning av python manage.py syncdb lokalt, men inget fel vid körning av samma kommando via Heroku