首页 > 解决方案 > 如何知道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>

标签: phpmysqlphpmyadmin

解决方案


您想要识别每一行上的最大值,以及包含该值的列的名称。

一个简单的方法使用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

推荐阅读