将平均值添加到mysql表的末尾

时间:2014-03-08 15:23:26

标签: php mysql

我有一张这样的表:

名称| 1 | 2 | 3 | 4 | 5 |

它在php中显示为表格

我如何计算每行中每个条目的平均值并将其添加到该表的末尾?

这是显示表格的代码:

<?php
$con=mysqli_connect("localhost","root","","education");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$result = mysqli_query($con,"SELECT * FROM students ((english+math+science)/3) as average");

echo "<table class=\"table1\" border=\"1\">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>English</th>
<th>Maths</th>
<th>Science</th>
<th>Average</th>
</tr>";

while($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['firstname'] . "</td>";
  echo "<td>" . $row['lastname'] . "</td>";
  echo "<td>" . $row['english'] . "</td>";
  echo "<td>" . $row['math'] . "</td>";
  echo "<td>" . $row['science'] . "</td>";
  echo "<td>" . $row['average'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysqli_close($con);
?>

1 个答案:

答案 0 :(得分:3)

您可以尝试以下方式:

SELECT name, one, two, three, four, five, ((one+two+three+four+five)/5) as average
FROM table