[英]MySQL : Getting the sum of multiple columns
This is very conflict for me let me explain to the best that I could please. 这对我来说是非常矛盾的,请允许我尽我所能解释。 This is like a 2 layers to compute.
这就像要计算的2层。 The first layer is I need to get the total A, and total B, and total C, and total D .
第一层是我需要获取总A,总B,总C和总D。 But, to get the total A is i need to compute some columns, as well as total B, C and D .
但是,要获得总数A,我需要计算一些列以及总数B,C和D。 The second layer is to display and get the sum of the total A ,B, C, D. This is a bunch of tables.
第二层是显示并获取总A,B,C,D的总和。这是一堆表。
For the first table. 对于第一个表。
tbl_criteria
------------------------
crit_id | criteria_name
16 | sports
17 | formal
18 | talent
19 | nothing
The tbl_criteria has a sub criteria 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
Third table the judges. 第三表评委。
tbl_judges
------------------------
judge_id | judge_name
61 | first
62 | second
63 | third
Table of contestant lets say 2 contestant 选手名单让说2选手
tbl_cotestant
-----------------------------------------------
con_id | contestant_number | contestant_name |
1 | 1 | john |
2 | 2 | sy |
Last table, This is the constructed table 最后一张桌子,这是构造好的桌子
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
The first layer output are something like this, getting the total A, B, C ,D. 第一层输出是这样的,得到总的A,B,C和D。
Total are should be display like 总计应显示为
(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
Table right above, John has got a total of 215 in Criteria number 16 (crit_id 16). 在上面的表格中,John在标准编号16(crit_id 16)中总共获得215。 As well as sy has got a total of 365 in criteria number 16.
sy总共有365个标准编号16。
So, i have 4 criteria in my table 16,17,18,19. 因此,我在表16,17,18,19中有4个条件。 This is my problem, that means i need to do query one by one to get each total output and that would be like
这是我的问题,这意味着我需要一个一个地查询以获取每个总输出,这就像
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
I need to do that in one query, I can do only if one by one getting total A, B C. But i need a one query that execute this kind of output. 我需要在一个查询中执行此操作,但只有在逐个获得总A,B C的情况下才可以执行。但是我需要一个执行这种输出的查询。 How can i achieve this output?
我如何获得此输出?
The total_a, b , c, d are equivalent to crit_id 16, 17, 18, 19 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
This query, I'm still learning about the logic 这个查询,我还在学习逻辑
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/cfe90/1
Getting the sum of only one total http://sqlfiddle.com/#!9/9efa5/1 仅获得一个总数的总和http://sqlfiddle.com/#!9/9efa5/1
Put your query in a subquery, and then add together the columns to get the grand total. 将您的查询放在子查询中,然后将各列加在一起以获得总计。
SELECT x.*, `total a` + `total b` + `total c` AS `Grand Total`
FROM (put your query here) AS x
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.