【问题标题】:how to know the max number in each row of mysql database and return the column name or return name in phpmysql如何知道mysql数据库每行中的最大数量并在phpmysql中返回列名或返回名称
【发布时间】:2020-09-19 07:25:50
【问题描述】:

如何知道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>

【问题讨论】:

    标签: php mysql phpmyadmin


    【解决方案1】:

    您想确定每一行的最大值,以及包含该值的列的名称。

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

    【讨论】:

    • 谢谢,但你能帮我写代码吗?如果下面是我的代码,我该如何添加我的代码以返回它
    猜你喜欢
    • 2022-11-30
    • 2020-09-18
    • 1970-01-01
    • 2017-10-28
    • 1970-01-01
    • 1970-01-01
    • 2013-05-22
    • 2019-06-13
    • 2021-11-05
    相关资源
    最近更新 更多