1

我需要计算学生获得的 3 个成绩的平均成绩。

ID      subject     G1  G2  G3
12345   Math        90  80  77
12345   Physics     99  89  78
12345   Network     76  60  90
99999   Math        50  90  88
99999   Chemistry   80  70  88
88888   English     90  90  100
88888   Physics     90  89  79

这些是 MySQL 数据库中的条目,我需要一种方法来计算 3 列之间每行的这些条目的平均值

所以当输出在网络上检索时,它看起来像这样

subject     gradeone    gradetwo    gradethree  average
Math        90          80          77  

任何帮助表示赞赏!非常感谢!

4

3 回答 3

7

例子

SELECT subject,
       g1 as gradeone,
       g2 as gradetwo,
       g3 as gradethree,
       (g1+g2+g3)/3 as average
FROM   students

结果:

SUBJECT     GRADEONE  GRADETWO  GRADETHREE  AVERAGE
Math        90        80        77          82.3333
Physics     99        89        78          88.6667
Network     76        60        90          75.3333
Math        50        90        88          76
Chemistry   80        70        88          79.3333
English     90        90        100         93.3333
Physics     90        89        79          86
于 2013-11-01T14:26:26.030 回答
3
SELECT subject,
       G1 AS gradeone,
       G2 AS gradetwo,
       G3 AS gradethree,
       ((G1 + G2 + G3) / 3) AS average
FROM   tablename;

假设您的考试成绩从不超过三个,这将为您提供每个学生想要的输出。

于 2013-11-01T14:23:54.763 回答
1

您可以使用 sql 之类的 -

select subject, 
       g1 as gradeone, 
       g2 as gradetwo, 
       g3 as gradethree, 
       ((g1+g2+g3)/3) as average 
from tablename where id=12345;
于 2013-11-01T14:24:44.707 回答