php - 使用 mysql 和 php 从虚拟值回显 AVG 值

标签 php mysql average

我有以下查询,它给出了百分比。它效果很好,我在表格中呼应了它。在此表中,我还想回显 AVG % 作为总计。即我一年有 12 个月,想要平均值。表格底部的年份百分比。我已经弄清楚如何对存储在我的数据库中的估价和说明执行此操作。但是..我该如何对像%这样的“虚拟”列执行此操作,因为它是根据查询创建的,而不是数据库中的物理列。

  • 我开始认为我需要将总估值与总指令相结合才能得到我的百分比。我有什么想法可以做到这一点吗?

    $query=“选择*, concat(round(( 指令/估值 * 100 ),0),'%') AS 百分比 来自office_figures2016”; $结果 = mysqli_query($conn, $query); while($office_figures2016=mysqli_fetch_assoc($result)){

    它回响:

    echo"".$office_figures2016['百分比']."";

这是获取每列总计的 php...代码的第三部分是错误的,但这是我到目前为止所拥有的:

<?php
$sql = "SELECT ROUND(AVG(valuations),0) AS value_sum FROM office_figures2016";

$result = $conn->query($sql);


if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {


        echo "<td> " . $row["value_sum"]."</td>";


$sql = "SELECT ROUND(AVG(instructions),0) AS value_sum FROM office_figures2016";

$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {


        echo "<td> " . $row["value_sum"]."</td>";


// need to divide total valuations by total instructions x 100 = % 
// somehow need to combine the value sums together..  if? 

$sql = "SELECT ROUND(AVG(''),0) AS value_sum FROM office_figures2016";

$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {


        echo "<td> " . $row["value_sum"]."</td>";

最佳答案

I redid the formula to divide all instructions and valuations in the table x100 = %.   

$sql = "SELECT ROUND(AVG(说明/估价 *100 ),0) AS value_sum FROM office_figures2016";

$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {


        echo "<td> " . $row["value_sum"]."</td>";

关于php - 使用 mysql 和 php 从虚拟值回显 AVG 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45683062/

相关文章:

mysql - 如何在数据库中存储时间/值数据向量

mysql - Ruby On Rails 很慢...?

mysql - 有什么办法可以更改此 SQL,使术语只定义一次吗?

Java,使用Math.random计算中位数和平均值

php - 将一个字符串与从 mysql 数据库中获取的另一个字符串匹配

php - MySQL - 'field' 没有默认值

php - 从 slickgrid 中保存数据

Excel:使用 LOOKUP 或 SUMPRODUCT 代替 AverageIFS

python - python文件中年和月数据的平均温度

php - VPS 上的 Apache 沙箱和文件上传