其他表mysql中孩子的总和价格

sum price of childs in other table mysql(其他表mysql中孩子的总和价格)
本文介绍了其他表mysql中孩子的总和价格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表一个存储数据子和父层次结构以及其他路径和后代

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

+----------+------------+-----------+
| userid   |    parent  |    price  |
+----------+------------+------------
| 1        |    null    |      20   | 
| 2        |      1     |      20   | 
| 3        |      1     |      20   | 
| 4        |      2     |      20   | 
| 5        |      2     |      20   | 
| 6        |      3     |      20   | 
| 7        |      4     |      20   | 
+----------+------------+-----------+

我需要获取父级 1 的所有用户 ID,然后在其他表中获取后代并按用户 ID 和价格分组

I need to get all userid with parent 1 then get descendant in other table and group by userid sum prices

+-------------+---------------+-------------+
| ancestor_id | descendant_id | path_length |
+-------------+---------------+-------------+
|           1 |             1 |           0 |
|           1 |             2 |           1 |
|           1 |             3 |           1 |
|           1 |             4 |           2 |
|           1 |             5 |           2 |
|           1 |             6 |           2 |
|           1 |             7 |           3 |
|           2 |             2 |           0 |
|           2 |             4 |           1 |
|           2 |             5 |           1 |
|           2 |             7 |           2 |
|           3 |             3 |           0 |
|           3 |             6 |           1 |
|           4 |             4 |           0 |
|           4 |             7 |           1 |
|           5 |             5 |           0 |
|           6 |             6 |           0 |
|           7 |             7 |           0 |
+-------------+---------------+-------------+

我已经查询它把所有孩子加在一起

I have query it sum all childs together

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 = 1 

这项工作正常,但总和父级 1

this work fine but total sum parent 1

我需要显示以下子级直接 (2,3) 的结果

I need to show bellow result for child direct (2,3)

 +----------+------------+-
    | userid   |    total   |
    +----------+------------+
    | 2        |    80      |
    | 3        |    40      |
    +----------+------------+

也在创建 sqlfiddle 我的问题 http://sqlfiddle.com/#!9/9415ed/2

also in create sqlfiddle my question http://sqlfiddle.com/#!9/9415ed/2

推荐答案

试试这个;)

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 演示

已编辑

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 演示

这篇关于其他表mysql中孩子的总和价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)