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;
            }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算