php - 如何知道mysql数据库每行的最大数量并在phpmysql中返回列名或返回名称
问题描述
如何知道mysql数据库每行的最大数量并在phpmysql中返回列名或返回名我希望占主导地位的行显示一行的最大值,该行有这一列(o_result,c_result,e_result,_result)
我的代码是
<tbody>
<?php
$result= mysqli_query($connection,"select * from test_report order by test_id ASC" ) or die (mysqli_error($connection));
while ($row= mysqli_fetch_array ($result) ){
$id=$row['test_id'];
?>
<tr>
<td><?php echo $row['test_id']; ?></td>
<td><?php echo $row['date_of_submission']; ?></td>
<td><?php echo $row['status']; ?></td>
<td><?php echo $row['o_result']; ?></td>
<td><?php echo $row['c_result']; ?></td>
<td><?php echo $row['e_result']; ?></td>
<td><?php echo $row['a_result']; ?></td>
<td><?php echo $row['n_result']; ?></td>
<td><?php echo $row['dominant']; ?></td>
<td><button class="btn search"><a href="view_participant.php<?php echo '?id='.$id; ?>" class="icon-search"></a></button>
<button class="btn edit"><a href="view_participant.php<?php echo '?id='.$id; ?>" class="icon-edit"></a></button>
<button class="btn btn-danger"><a href="crud/delete_part.php<?php echo '?id='.$id; ?>" class="icon-remove"></a></button>
</td>
</tr>
<?php } ?>
</tbody>
解决方案
您想要识别每一行上的最大值,以及包含该值的列的名称。
一个简单的方法使用greatest()
and case
:
select
t.*,
greatest(o_result, c_result, e_result, a_result, n_result) as max_result,
case greatest(o_result, c_result, e_result, a_result, n_result)
when o_result then 'o_result'
when c_result then 'c_result'
when a_result then 'a_result'
when n_result then 'n_result'
end as max_column_name
from mytable t
推荐阅读
- mysql - POST http://localhost:3307/insert 500(内部服务器错误)
- jenkins - 由于许可,无法使用 Helm 在 Minikube 上安装 Jenkins
- python - 使用 FeedParser 导入 RSS 并将帖子和一般信息获取到单个 Pandas DataFrame
- javascript - 在机器人框架 selenium2library 中保存 javascript 的返回值给出 0
- python - 不使用函数按升序对三个数字进行排序
- android - 在 Graphql 突变上获取内部服务器错误 500
- javascript - 删除属性和类
- azure - Azure API 管理速率限制(按 Json 正文)
- python - 有没有办法让不和谐的机器人屏幕共享网站
- android - 显示键盘时全屏 AlertDialog 有 janky 动画