MySQL:获取多列的总和

时间:2016-09-15 21:07:46

标签: php mysql

这对我来说是非常矛盾的,让我尽力解释。这就像要计算的2层。第一层是我需要得到总A,总B,总C和总D。但是,要获得总A,我需要计算一些列,以及总B,C和D.第二层是显示并获得总A,B,C,D的总和。这是一堆表。

第一张表。

 tbl_criteria
 ------------------------
 crit_id | criteria_name
    16   |    sports
    17   |    formal
    18   |    talent
    19   |    nothing

tbl_criteria有子标准

 tbl_sub_criteria
 ----------------------
 sub_crit_id | crit_id | sub_crit_name
      22     |   16    |    originality
      23     |   16    |    audience Impact
      24     |   18    |    Appeal
      25     |   18    |    Stage Presence

第三表评委。

tbl_judges
 ------------------------
judge_id  |   judge_name
   61     |    first
   62     |    second
   63     |    third

参赛者表让2名选手

tbl_cotestant
-----------------------------------------------
con_id  |  contestant_number | contestant_name |
   1    |         1          |      john       |
   2    |         2          |       sy        |

最后一张表,这是构造的表

tbl_score
--------------------------------------------------
score_id | crit_id |  sub_crit_id |   judge_id | con_id | contestant_number |   score
   1     |    16   |      22      |      61    |   1    |        1          |     25  
   2     |    16   |      22      |      61    |   2    |        2          |     25
   3     |    16   |      22      |      62    |   1    |        1          |     25  
   4     |    16   |      22      |      62    |   2    |        2          |     73
   5     |    16   |      22      |      63    |   1    |        1          |     70  
   6     |    16   |      22      |      63    |   2    |        2          |     80
   7     |    16   |      23      |      61    |   1    |        1          |     25  
   8     |    16   |      23      |      61    |   2    |        2          |     25
   9     |    16   |      23      |      62    |   1    |        1          |     25  
   10    |    16   |      23      |      62    |   2    |        2          |     73
   11    |    18   |      23      |      63    |   1    |        1          |     70   
   12    |    16   |      23      |      63    |   2    |        2          |     80
   13    |    18   |      24      |      61    |   1    |        1          |     25  
   14    |    18   |      24      |      61    |   2    |        2          |     25
   15    |    18   |      24      |      62    |   1    |        1          |     25  
   16    |    18   |      24      |      62    |   2    |        2          |     73
   17    |    18   |      24      |      63    |   1    |        1          |     70  
   18    |    18   |      24      |      63    |   2    |        2          |     80
   19    |    18   |      25      |      61    |   1    |        1          |     25  
   20    |    18   |      25      |      61    |   2    |        2          |     25
   21    |    18   |      25      |      62    |   1    |        1          |     25  
   22    |    18   |      25      |      62    |   2    |        2          |     73
   23    |    18   |      25      |      63    |   1    |        1          |     70  
   24    |    18   |      25      |      63    |   2    |        2          |     80
   25    |    17   |     null     |      61    |   1    |        1          |     25  
   26    |    17   |     null     |      61    |   2    |        2          |     25
   27    |    17   |     null     |      62    |   1    |        1          |     25  
   28    |    17   |     null     |      62    |   2    |        2          |     73
   29    |    17   |     null     |      63    |   1    |        1          |     70  
   30    |    17   |     null     |      63    |   2    |        2          |     80
   31    |    19   |     null     |      61    |   1    |        1          |     25  
   32    |    19   |     null     |      61    |   2    |        2          |     25
   33    |    19   |     null     |      62    |   1    |        1          |     25  
   34    |    19   |     null     |      62    |   2    |        2          |     73
   35    |    19   |     null     |      63    |   1    |        1          |     70  
   36    |    19   |     null     |      63    |   2    |        2          |     80

第一层输出是这样的,得到总A,B,C,D。

总计应显示为

(criteria 16 has two sub-criterias 22, 23 , that means it will be x2)
con_num | contestant_name | 16_judge_61 | 16_judge_62 | 16_judge_63 | total a
    1   |       john      |       50    |      25     |     140     |   215   
    2   |       sy        |       50    |      146     |     160      |  365

上面的表格中,John在Criteria number 16(crit_id 16)中共得到215个。同样,sy在标准编号16中总共有365个。

所以,我的表16,17,18,19中有4个标准。这是我的问题,这意味着我需要逐个查询以获得每个总输出,这就像

con_num | contestant_name | 17_judge_61 | 17_judge_62 | 17_judge_63 | total b
    1   |       john      |       25    |      25     |     70      |  120     
    2   |       sy        |       25    |      73     |     80      |  178

 (criteria 18 has a sub criteria 24,25 that means it will x2)
 con_num | contestant_name | 18_judge_61 | 18_judge_62 | 18_judge_63 | total c
    1   |       john      |       50    |      50     |     140      |  240     
    2   |       sy        |       50    |      146    |     160      |  356


 con_num | contestant_name | 19_judge_61 | 19_judge_62 | 19_judge_63 | total d
    1   |       john      |       25    |      25     |     70      |  120     
    2   |       sy        |       25    |      73     |     80      |  178

我需要在一个查询中执行此操作,我只能逐个获得总计A,B C.但我需要一个执行此类输出的查询。我怎样才能实现这一输出?

total_a,b,c,d相当于crit_id 16,17,18,19

con_num | contestant_name | 16total_a | 17total_b | 18total_c | 19total_d | Grand_total
   1    |       john      |   215     |   120     |   240     |  120      |   695 
   2    |        jy       |   365     |   178     |   356     |  178      |   1077 

这个查询,我还在学习逻辑

 SELECT DISTINCT(a.contestant_number) as con_num, a.contestant_name,

//Getting first the sum of total a, Notice criteria 16 has a sub criteria 22, 23
SUM(CASE WHEN s.crit_id='16' AND s.sub_crit_id = 22 AND s.judge_id='61' THEN s.score END) as 16_judge_61,
SUM(CASE WHEN s.crit_id='16' AND s.sub_crit_id = 22 AND s.judge_id='62' THEN s.score END) as 16_judge_62,
SUM(CASE WHEN s.crit_id='16' AND s.sub_crit_id = 22 AND s.judge_id='63' THEN s.score END) as 16_judge_63,

SUM(CASE WHEN s.crit_id='16' AND s.sub_crit_id = 23 AND s.judge_id='61' THEN s.score END) as 16_judge_61,
SUM(CASE WHEN s.crit_id='16' AND s.sub_crit_id = 23 AND s.judge_id='62' THEN s.score END) as 16_judge_62,
SUM(CASE WHEN s.crit_id='16' AND s.sub_crit_id = 23 AND s.judge_id='63' THEN s.score END) as 16_judge_63,

SUM(CASE WHEN s.crit_id='16' AND s.judge_id in (61, 62, 63) THEN s.score END) as 'total a'

//Criteria 17 has no sub criteria                                                    
SUM(CASE WHEN s.crit_id='17' AND s.judge_id='61' THEN s.score END) as 16_judge_61,
SUM(CASE WHEN s.crit_id='17' AND s.judge_id='62' THEN s.score END) as 16_judge_62,
SUM(CASE WHEN s.crit_id='17' AND s.judge_id='63' THEN s.score END) as 16_judge_63,


SUM(CASE WHEN s.crit_id='17' AND s.judge_id in (61, 62, 63) THEN s.score END) as 'total b'                                                    

//And iterate again for the 2 more criteria
//
FROM tbl_score s
INNER JOIN tbl_contestant a ON s.contestant_number = a.contestant_number
INNER JOIN tbl_judges j ON j.judge_id = s.judge_id
WHERE c.gender = 'male' and c.con_id = s.con_id
GROUP BY s.contestant_number
ORDER By `Grand toal' DESC";                                                   

http://sqlfiddle.com/#!9/cfe90/1

获得总数的总和 http://sqlfiddle.com/#!9/9efa5/1

1 个答案:

答案 0 :(得分:1)

将您的查询放在子查询中,然后将列添加到一起以获得总计。

SELECT x.*, `total a` + `total b` + `total c` AS `Grand Total`
FROM (put your query here) AS x