duanqiao2225 2017-04-05 19:59
浏览 6
已采纳

DB数据中的输出更改

I have a database with some data.

for example:

Time    Type
1:00    123
2:00    123
3:00    123
4:00    123
5:00    113
6:00    113
7:00    113
8:00    113
9:00    334
10:00   334
11:00   334
12:00   123

And I would like to have an output from DB something like so:

At 5:00 type changed from 123 to 113
At 9:00 type changed from 113 to 334
At 12:00 type changed from 334 to 123

I've tried that via GROUP BY, but that outputted only different values, so it was OK, but when there were same date types, it outputted only the first change, because it was grouped only into one, of course...

Could anyone help me, please?

  • 写回答

1条回答 默认 最新

  • dongwen7283 2017-04-05 20:17
    关注

    Don't forget to mark this answer as "solved" if this solves your problem.

    <?php
    $servername = "localhost";
    $username = "username";
    $password = "password";
    $dbname = "myDB";
    
    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
            } 
    
    $sql = "SELECT Time, Type FROM MyTable";
    $result = $conn->query($sql);
    
    // Declare 4 variables to store time and type so you can compare to previous.
    $time1 = "";
    $type1 = "";
    $time2 = "";
    $type2 = "";
    
    if ($result->num_rows > 0) {
        // output data of each row
        while($row = $result->fetch_assoc()) {
            $time2 = $row["Time"];
            $type2 = $row["Type"];
            if ($type2 == $type1) {
                $time1 = $time2;
                $type1 = $type2;
                }
            else {
                echo "At $tim2 type changed from $type1 to $type2";
                $time1 = $time2;
                $type1 = $type2;
                }
            }
        }
    else {
        echo "0 results";
        }
    $conn->close();
    ?>
    

    You can shorten the code for the loop and write this:

        while($row = $result->fetch_assoc()) {
            $time2 = $row["Time"];
            $type2 = $row["Type"];
            if ($type2 != $type1) {
                echo "At $tim2 type changed from $type1 to $type2";
                }
            $time1 = $time2;
            $type1 = $type2;
            }
    

    To exclude the first result when the loop starts, write this:

        while($row = $result->fetch_assoc()) {
            $time2 = $row["Time"];
            $type2 = $row["Type"];
            if ($type2 != $type1 && $type1 != "") {
                echo "At $tim2 type changed from $type1 to $type2";
                }
            $time1 = $time2;
            $type1 = $type2;
            }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大