如何SUM数据表的每个表值

时间:2012-12-20 11:05:39

标签: mysql

我有四张桌子,需要帮助每张桌子的每张价格总和

1表skom

 _________________________________
 | id | noskom      | price_skom |
 |____|_____________|____________|
 |  1 | 1           |   10       | 
 |  2 | 2           |   30       |  
 |__3_|_3___________|___50_______|

2表skop

 ______________________________________________
 | id | noskom      | noskop     | price_skop |
 |____|_____________|____________|____________|
 |  1 | 1           |   1        |   10       |  
 |  2 | 1           |   2        |   20       |  
 |  3 | 2           |   3        |    5       | 
 |  4 | 2           |   4        |   30       |  
 |____|_____________|____________|____________|

3表npbm

 ___________________________________________________________
 | id | noskom      | noskop     | nonpbm     | price_npbm |
 |____|_____________|____________|____________|____________|
 |  1 | 1           |   1        |   1        |   10       | 
 |  2 | 1           |   1        |   2        |   20       |   
 |  3 | 1           |   2        |   3        |    5       | 
 |  4 | 1           |   2        |   4        |    5       | 
 |  5 | 2           |   1        |   5        |   10       |   
 |  6 | 2           |   2        |   6        |   10       |  
 |  7 | 2           |   2        |   7        |   10       |       
 |____|_____________|____________|____________|____________|

4表npbp

 ________________________________________________________________________
 | id | noskom      | noskop     | nonpbm     | nonpbp     | price_npbp |
 |____|_____________|____________|____________|____________|____________|
 |  1 | 1           |   1        |   1        |    1       |   10       | 
 |  2 | 1           |   1        |   2        |    2       |   20       |   
 |  3 | 1           |   2        |   3        |    3       |    5       | 
 |  4 | 1           |   2        |   4        |    4       |    5       | 
 |  5 | 2           |   1        |   5        |    5       |   10       |   
 |  6 | 2           |   2        |   6        |    6       |   10       |  
 |  7 | 2           |   2        |   7        |   10       |   10       |       
 |____|_____________|____________|____________|____________|____________|

我需要像这样的查询帮助

 select sum(price_skom),sum(price_skop),sum(price_npbm),sum(price_npbp) from each table group by noskom 

我想要这样的结果

表结果分组依据noskom

 ________________________________________________________________
 | id | noskom      | sum_skom | sum_skop | sum_npbm | sum_npbp |
 |____|_____________|__________|__________|__________|__________|
 |  1 | 1           |   10     |   30     |    40    |   90     |  
 |  2 | 2           |   30     |   35     |    30    |   70     |      
 |__3_|_3___________|___50_____|__________|__________|__________|

帮我解决mysql查询

2 个答案:

答案 0 :(得分:1)

要获得没有相应noscom的行,您需要有一个外部联接:

select sk1.id, sk1.noskom,sum(price_skom),sum(price_skop),sum(price_npbm),sum(price_npbp) 
from skom sk1 left outer join skop on (sk1.id=skop.noskom),
     skom sk2 left outer join npbm on (sk2.id=npbm.noskom),
     skom sk3 left outer join npbp on (sk3.id=npbp.noskom)
Where sk1.id=sk2.id and sk2.id=sk3.id
group by  sk1.id,sk1.noskom

答案 1 :(得分:1)

这样的事情:

select 
noskom, 
sum (pskom) as sum_skom, 
sum (pskop) as sum_skop, 
sum (pnpbm) as sum_npbm, 
sum (pnpbp) as sum_npbp

from
(
select noskom, sum (price_skom) as pskom, 0 as pskop, 0 as pnpbm, 0 as pnpbp from skom group by noskom
union all
select noskom, 0, sum (price_skop), 0, 0 from skop group by noskom
union all
select noskom, 0, 0, sum (price_npbm), 0 from npbm group by noskom
union all
select noskom, 0, 0, 0, sum (price_npbp) from npbp group by noskom
) as p

group by noskom