简体   繁体   English

如何在mysql中找到行总和?

[英]How to find Total of sum of rows in mysql?

I am trying to get all rows from table. 我试图从表中获取所有行。 I could do that job properly. 我可以做得很好。 But the problem comes when I put each row values as percentage and tried to sum the percentage. 但是问题出在我将每一行值作为百分比并尝试对百分比求和时。 For example, I am using the following query to output all the rows: 例如,我使用以下查询输出所有行:

SELECT SUM( Mark_score ) / SUM( Full_mark ) *50 AS Score
FROM nuexam
WHERE regd = '22'
GROUP BY subject

With the above query I got the following output: 通过上面的查询,我得到以下输出:

查询输出

I want to sum total of this Score . 我想对这个Score进行总计。 This is so far what I could output. 到目前为止,这是我可以输出的。 Any help is very much appreciated. 很感谢任何形式的帮助。 I will also welcome the php solution. 我也欢迎php解决方案。

Simply running a nested query should work: 只需运行嵌套查询即可:

SELECT SUM(s) as Score
FROM (SELECT SUM( Mark_score ) / SUM( Full_mark ) *50 AS s
    FROM nuexam
    WHERE regd = '22'
    GROUP BY subject) table

If you want to get the individual scores along with the total, then use with rollup . 如果您想获得单个分数以及总分,请with rollup 一起使用。 The proper way for your data would be: 数据的正确方法是:

select coalesce(subject, 'Total'), sum(t.Score)
from (SELECT subject, SUM( Mark_score ) / SUM( Full_mark ) *50 AS Score
      FROM nuexam
      WHERE regd = '22'
      GROUP BY subject
     ) t
group by subject with rollup;

http://sqlfiddle.com/#!2/a505a/4 http://sqlfiddle.com/#!2/a505a/4

select sum(t.Score) from 
(SELECT SUM( Mark_score ) / SUM( Full_mark ) *50 AS Score
FROM nuexam
WHERE regd = '22'
GROUP BY subject ) t

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM