skip在mysql查询中显示重复字段(不是记录)

ivqmmu1c  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(277)

我的代码只是查询数据库并输出到html表:

<?php

  include("incl\dbcon.php");

  $sql = "SELECT * FROM attendanceRecord";

  $result = $db_con->query($sql);

  echo "<table>
  <tr>
  <th>Date</th>
  <th>Building Name</th>
  <th>Room Name</th>
  <th>Student</th>
  <th>Time</th>
  </tr>";

  while($row_result = $result->fetch_assoc()){
    echo "<tr>";
    echo "<td>" . $row_result['rDate'] . "</td>";
    echo "<td>" . $row_result['rBuildingName'] . "</td>";
    echo "<td>" . $row_result['rRoomName'] . "</td>";
    echo "<td>" . $row_result['rStudent'] . "</td>";
    echo "<td>" . $row_result['rTime'] . "</td>";
    echo "</tr>";
  }

  $db_con->close();

  echo "</table>";

?>

结果如下:

Date         Building Name    Room Name  Student    Time
2018-07-12   Building A       1A         Sam        08:32:33
2018-07-12   Building A       1A         David      08:54:21
2018-07-12   Building A       1A         Dragon     08:50:10
2018-07-12   Building A       1B         John       08:43:11
2018-07-12   Building A       1B         Coco       08:51:39
2018-07-12   Building B       3A         Mary       08:21:23
2018-07-12   Building B       3A         Martin     08:46:57
2018-07-12   Building B       4B         Ray        08:26:47

我怎样才能不显示或清空一行中的重复字段,并使表如下所示?

Date         Building Name    Room Name  Student    Time
2018-07-12   Building A       1A         Sam        08:32:33
                                         David      08:54:21
                                         Dragon     08:50:10
                              1B         John       08:43:11
                                         Coco       08:51:39
             Building B       3A         Mary       08:21:23
                                         Martin     08:46:57
                              4B         Ray        08:26:47

我应该去哪里?从sql查询(不知道查询时是否有空或空重复字段的函数)或php数组(如果重复则打印空然后移动指针?),请帮助,提前感谢!

tgabmvqs

tgabmvqs1#

假设行总是以正确的顺序出现(可以使用order by来确保!)然后在php中,保留一个变量,该变量包含前一行的日期值。如果它与当前行上的日期匹配,则不要在td中打印该值。如果没有,那就打印出来,因为它一定是新的。建筑物名称和房间名称的方法相同,但在当前日期(和建筑物)的上下文中:

$date = "";
$buildingName = ""; //declare these outside the loop so they persist between loops
$roomName = "";

while($row_result = $result->fetch_assoc()){
    echo "<tr>";
    echo "<td>" . ($row_result['rDate'] == $date ? "" : $row_result['rDate']) . "</td>";
    echo "<td>" .($row_result['rBuildingName'] == $buildingName $row_result['rDate'] == $date ? "" : $row_result['rBuildingName']) . "</td>";
    echo "<td>" . ($row_result['rRoomName'] == $roomName && $row_result['rBuildingName'] == $buildingName && $row_result['rDate'] == $date ? "" : $row_result['rRoomName']) . "</td>";
    echo "<td>" . $row_result['rStudent'] . "</td>";
    echo "<td>" . $row_result['rTime'] . "</td>";
    echo "</tr>";
    //update the references to the specific fields
    $date = $row_result['rDate'];
    $buildingName = $row_result['rRoomName'];
    $roomName = $row_result['rRoomName'];
}

p、 我绝不会尝试在sql中这样做,因为它实际上是特定于这个特定上下文中数据的表示,而不是数据本身的性质。在php中也简单得多。

hyrbngr7

hyrbngr72#

在php中,您可以这样做

include("incl\dbcon.php");

  $sql = "SELECT * FROM attendanceRecord";

  $result = $db_con->query($sql);

  echo "<table>
  <tr>
  <th>Date</th>
  <th>Building Name</th>
  <th>Room Name</th>
  <th>Student</th>
  <th>Time</th>
  </tr>";

  $rDate = $rBuildingName = $rRoomName = $rStudent = $rTime = [];

  while($row_result = $result->fetch_assoc()){

    echo "<tr>";
    echo "<td>" . (!in_array(  $row_result['rDate'] , $rDate) ? $row_result['rDate'] : ''). "</td>";
    echo "<td>" . (!in_array(  $row_result['rBuildingName'] , $rBuildingName) ? $row_result['rBuildingName'] : '') . "</td>";
    echo "<td>" . (!in_array(  $row_result['rRoomName'] , $rRoomName) ? $row_result['rRoomName'] : '') . "</td>";
    echo "<td>" . (!in_array(  $row_result['rStudent'] , $rStudent) ? $row_result['rStudent'] : '') . "</td>";
    echo "<td>" . (!in_array(  $row_result['rTime'] , $rTime) ? $row_result['rTime'] : '') . "</td>";
    echo "</tr>";

    $rDate[] = $row_result['rDate'];
    $rBuildingName[] = $row_result['rBuildingName'];
    $rRoomName[] = $row_result['rRoomName'];
    $rStudent[] = $row_result['rStudent'];
    $rTime[] = $row_result['rTime'];

  }

  $db_con->close();

  echo "</table>";

相关问题