Php mysql中列之间的平均值

Php mysql中列之间的平均值,php,html,mysql,mysqli,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 Phys

我需要计算学生获得的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列之间每行这些条目的平均值

因此,当输出在web上检索时,它如下所示

subject     gradeone    gradetwo    gradethree  average
Math        90          80          77  
感谢您的帮助!非常感谢

SELECT subject,
       G1 AS gradeone,
       G2 AS gradetwo,
       G3 AS gradethree,
       ((G1 + G2 + G3) / 3) AS average
FROM   tablename;
假设你的考试分数从未超过三分,这将为你提供每个学生想要的输出

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

您可以像sql一样使用sql-

select subject, 
       g1 as gradeone, 
       g2 as gradetwo, 
       g3 as gradethree, 
       ((g1+g2+g3)/3) as average 
from tablename where id=12345;
您可以像sql一样使用sql-

select subject, 
       g1 as gradeone, 
       g2 as gradetwo, 
       g3 as gradethree, 
       ((g1+g2+g3)/3) as average 
from tablename where id=12345;

结果:

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

结果:

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

普罗蒂普:看看函数。@BradChristie看了看。。如果你总是能
(g1+g2+g3)/3
@bradcristie
AVG
只适用于列averages@SavTheCoder:很抱歉,我假设没有3个固定的Gn列。protip:看看函数。@BradChristie看了看。。如果你总是能
(g1+g2+g3)/3
@bradcristie
AVG
只适用于列averages@SavTheCoder:对不起,但我假设没有3个固定的Gn列。@Brad-我只是好奇为什么标题都是大写的。@Fred ii-:SqlFiddle是本机生成的-不知道为什么。@BradChristie我是这么想的,我要编辑我的评论,说它是否是它所在服务器的专有信息;多谢各位+1对于你的答案,顺便说一句,像这样的答案/问题可以证明是非常有用的,干杯。@TimValishin:如果你想有更好的方式来组织信息,而不是只关注三个等级:@Brad-我只是好奇为什么标题都是大写的。@Fred ii-:SqlFiddle是天生的-不确定为什么。@BradChristie我就是这么想的,我打算编辑我的评论,如果它是它所在服务器的专有信息;多谢各位+1对于你的答案,顺便说一句,像这样的答案/问题可以证明是非常有用的,干杯。@TimValishin:如果你曾经想要一种更好的方式来组织信息,而不是只关注三个等级: