dongyuxiao6295 2018-07-21 15:15
浏览 33
已采纳

删除相同的获取数据PHP

<?php
$con = mysqli_connect("localhost","root","","final_osa");
$s_stud = $con->query("SELECT * FROM violations_tbl GROUP BY violation_type");
while($data = $s_stud->fetch_assoc() ){
    $bilang = $con->query("SELECT COUNT(*) FROM violations_tbl WHERE `violation_type` ='".$data['violation_type']."' ");
    $result = $bilang->fetch_assoc();
    if($result['COUNT(*)'] > 1 ){
        echo "<tr>";
            echo "<td>";
                $query=$con->query("SELECT `violation_type` FROM `violations_tbl` WHERE `violation_type`='".$data['violation_type']."'");
                while($row=$query->fetch_assoc() ){
                    echo $row['violation_type'].", ";
                }
                echo "</td>";
                echo "</tr>";
    }
} 
?>

How can i eliminate same fetched data and echo only one? thanks

here is the one that it echoes. it should be that it will echo only one because its the sameenter image description here

What i'm trying to do here is get the mos violated rule in school thanks

  • 写回答

1条回答 默认 最新

  • doutu4335 2018-07-21 15:38
    关注

    If you want all the violations in order of most violated to least violated

    $s_stud = $con->query("SELECT violation_type, count(violation_type) as num_violations
                          FROM violations_tbl 
                          GROUP BY violation_type
                          ORDER BY num_violations DESC");
    
    while($row= $s_stud->fetch_assoc() ){
        //echo the violation and the count
        if($row['num_violations'] > 1 ){
            echo "<tr>";
            echo "<td>$row[violation_type]</td>";
            echo "<td>$row[num_violations]</td>";
            echo "</tr>";
        }
    }
    

    If you only want the MOST violated you could add a LIMIT 1 to the query and remove the looping.

    $s_stud = $con->query("SELECT violation_type, count(violation_type) as num_violations
                          FROM violations_tbl 
                          GROUP BY violation_type
                          ORDER BY num_violations DESC
                          LIMIT 1");
    
    $row= $s_stud->fetch_assoc();
    //echo the violation and the count
    echo "<tr>";
    echo "<td>$row[violation_type]</td>";
    echo "<td>$row[num_violations]</td>";
    echo "</td></tr>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)