从不同的表的子行的总和与加入

时间:2015-04-15 10:59:18

标签: mysql mysqli

Table 1 - 
    i_rec_id 
    i_parent_id
    i_type

Table 2 -
    i_rec_id
    i_ref_wbs_id
    vc_plan

表1-如果行中有父项,则i_parent_id包含该行的i_rec_id,

i_ref_wbs_id是tabel 1 i_rec_id的外键。

问题:我想选择所有记录,但如果i_type为1则应显示其子行vc_plan的总和vc_plan

Sample Record :

Table 1 -  
i_rec_id     i_parent_id   i_type
1             NULL          1
2             1             2
3             1             2
4             NULL          1
5             1             3
6             4             2

Table 2 
i_rec_id     i_ref_wbs_id   vc_plan
10             1             NULL
21             2             2
31             3             2
42             4             NULL
56             5             3
62             6             2

预期产出:

i_rec_id     i_parent_id   i_type   vc_plan
1             NULL          1        7
2             1             2        2
3             1             2        2
4             NULL          1        2
5             1             3        3
6             4             2        2

1 个答案:

答案 0 :(得分:0)

这有点复杂的计算。 这是一种方法,但也有一些更好的方法,我现在无法想到。

select 
t1.*,
if(t1.i_type = 1 , sum(t3.vc_plan),t3.vc_plan) as vc_plan  
from table1 t1 
left join table1 t2 on t2.i_parent_id = t1.i_rec_id 
left join table2 t3 on t3.i_ref_wbs_id = t2.i_rec_id 
or t3.i_ref_wbs_id = t1.i_rec_id 
group by t1.i_rec_id ;

以下是问题中提供的数据集和预期输出的测试用例

mysql> select * from table1 ;
+----------+-------------+--------+
| i_rec_id | i_parent_id | i_type |
+----------+-------------+--------+
|        1 |        NULL |      1 |
|        2 |           1 |      2 |
|        3 |           1 |      2 |
|        4 |        NULL |      1 |
|        5 |           1 |      3 |
|        6 |           4 |      2 |
+----------+-------------+--------+
6 rows in set (0.00 sec)

mysql> select * from table2;
+----------+--------------+---------+
| i_rec_id | i_ref_wbs_id | vc_plan |
+----------+--------------+---------+
|       10 |            1 |    NULL |
|       21 |            2 |       2 |
|       31 |            3 |       2 |
|       42 |            4 |    NULL |
|       56 |            5 |       3 |
|       62 |            6 |       2 |
+----------+--------------+---------+
6 rows in set (0.00 sec)

mysql> select 
    -> t1.*,
    -> if(t1.i_type = 1 , sum(t3.vc_plan),t3.vc_plan) as vc_plan  
    -> from table1 t1 
    -> left join table1 t2 on t2.i_parent_id = t1.i_rec_id 
    -> left join table2 t3 on t3.i_ref_wbs_id = t2.i_rec_id 
    -> or t3.i_ref_wbs_id = t1.i_rec_id 
    -> group by t1.i_rec_id ;
+----------+-------------+--------+---------+
| i_rec_id | i_parent_id | i_type | vc_plan |
+----------+-------------+--------+---------+
|        1 |        NULL |      1 |       7 |
|        2 |           1 |      2 |       2 |
|        3 |           1 |      2 |       2 |
|        4 |        NULL |      1 |       2 |
|        5 |           1 |      3 |       3 |
|        6 |           4 |      2 |       2 |
+----------+-------------+-----

---+---------+

6 rows in set (0.00 sec)