【问题标题】:sum price of childs in other table mysql其他表mysql中孩子的总和价格
【发布时间】:2023-03-15 18:25:02
【问题描述】:

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

+----------+------------+-----------+
| 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 和价格分组

+-------------+---------------+-------------+
| 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 |
+-------------+---------------+-------------+

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

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

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

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

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

【问题讨论】:

    标签: mysql sql join group-by sum


    【解决方案1】:

    试试这个;)

    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

    已编辑

    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

    【讨论】:

    • @VahidAlvandi 好的,让我们使用JOIN,请检查我的已编辑答案。
    • 干得好,我需要一本像你一样的mysql教程..请自我介绍一下
    • 哇,你想要这个tutorial
    • 不错! ,我如何将此查询与您的答案结合起来stackoverflow.com/a/37877231/6477873 第二个查询有另一个表与您的答案结果排名
    • 不是很清楚你上面说的。 :-(@VahidAlvandi
    【解决方案2】:

    试试这个

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-22
      • 2018-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多