簡體   English   中英

MySQL:獲取多列之和

[英]MySQL : Getting the sum of multiple columns

這對我來說是非常矛盾的,請允許我盡我所能解釋。 這就像要計算的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在標准編號16(crit_id 16)中總共獲得215。 sy總共有365個標准編號16。

因此,我在表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

將您的查詢放在子查詢中,然后將各列加在一起以獲得總計。

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM