gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 12:15:49 25 4
gpt4 key购买 nike

我有一张这样的 table :

名字 | 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);
?>

最佳答案

你可以尝试这样的事情:

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

关于php - 将平均值添加到 mysql 表的末尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22271159/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com