mysql中列之间的平均值

时间:2013-11-01 14:17:07

标签: php html mysql mysqli

我需要计算学生收到的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  

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

3 个答案:

答案 0 :(得分:7)

Example

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

答案 1 :(得分:3)

SELECT subject,
       G1 AS gradeone,
       G2 AS gradetwo,
       G3 AS gradethree,
       ((G1 + G2 + G3) / 3) AS average
FROM   tablename;

假设您的测试成绩从未超过三个,那么这将为您提供每个学生所需的输出。

答案 2 :(得分:1)

你可以使用像 -

这样的sql
select subject, 
       g1 as gradeone, 
       g2 as gradetwo, 
       g3 as gradethree, 
       ((g1+g2+g3)/3) as average 
from tablename where id=12345;
相关问题