mysql - 突出显示表 html 中的一行,该行的值超过平均值

标签 mysql

我有一张看起来像这样的 table 。 MyTable

我想要做的是,我想突出显示(黄色)哪一行的每 1K 使用量高于平均值。结果应该是这样的。 enter image description here

这是我的代码,用于在超过平均值时突出显示一行,但我无法将值 if($row['duh']>'0.77') 更改为 if($row['duh']>'$avg') 因为它会突出显示所有行。

$no   = 1;
while ($row = mysqli_fetch_array($result))
{
  $month1=strtotime($row['month']);
  $month=date('m-Y',$month1);
  $store_name=$row['store_name'];
  $netamt1=$row['netamt'];
  $netamt11=number_format($netamt1,2);
  $usage1=$row['monusage'];
  $usage11=number_format($usage1,2);
  $answer=$usage1/$netamt1*1000;
  $answer1=number_format($answer,2);
  $total+=$answer1;
  $avg=$total/$no;
  $duh = number_format($row['duh'],2);

   echo "<tr>"; 
   echo "<td>".$no."</td>"; 
   echo "<td>".$store_name."</td>"; 
   echo "<td>".$date2111."</td>";
   echo "<td>".$netamt11."</td>"; 
   echo "<td>".$usage11."</td>";  
   if($row['duh']>'0.77') {// 0.77 i cannot change to $avg, because it will hightlight all the rows become yellow
   echo "<td style='background-color: #FFFF00;'>".$duh."</td>";}
   else {
       echo "<td>".$duh."</td>";
    } 
   echo "</tr>"; 
  $no++;
}?>
</tbody>
<tfoot>
  <tr>
    <th colspan="5">TOTAL</th>
    <th><?=($total)?></th>
  </tr>
  <tr>
  <th colspan="5">AVERAGE</th>
  <th><?=number_format($avg,4)?></th>
  </tr>
</tfoot>

最佳答案

您可以将 average 添加为查询结果中的另一列,并将值与其进行比较,例如:

SELECT no, outlet, date, sales, usage, usage_per_1k, AVG(usage_per_1k) AS avg
FROM table
WHERE condition;

然后,添加以下内容以突出显示:

if($row['duh']>$row['avg']) {
   hightlight all the rows become yellow
   echo "<td style='background-color: #FFFF00;'>".$duh."</td>";
}

关于mysql - 突出显示表 html 中的一行,该行的值超过平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45028481/

相关文章:

php - 如何选择一列中的最大值,然后选择另一列中的最大值?

MySQL 错误 - 您必须设置密码/密码哈希应该是 41 位十六进制数

mysql - 相同的 MySQL 查询在 MySQL Workbench 中返回与命令行不同的结果

sed - 从 mysqldump 中按表名获取 INSERT 的行数 - awk、sed、grep

mysql - 从连接表中选择全部和平均值

java - 如何让任意IP地址连接到远程MySQL服务器?

mysql - SELECT DISTINCT 多个字段并全部显示

mysql - 外键还是复合键?

php - MySQL 查询中的性能

java - MySQL DATETIME 精度(joda-time、Hibernate、org.jadira.usertype、hbm2ddl)