如何“使用”当前行的前一行中的mysql单元格,并在一条sql语句中显示包括第一行在内的所有行?

relj7zay  于 2021-06-19  发布在  Mysql
关注(0)|答案(1)|浏览(291)

这个问题在这里已经有了答案

第一个html行未显示(1个答案)
两年前关门了。
假设我有一个包含动态添加内容的html表,需要如下所示:

+----+---------------------+---------------------+--------------------------+-----------------------------+
| ID |          A          |          B          |            C             | Elapsed since last checkout |
+----+---------------------+---------------------+--------------------------+-----------------------------+
|  1 | 2018-09-01 00:00:00 | 2018-09-03 00:00:00 | 2 days 0 hours 0 minutes | unknown                     |
|  2 | 2018-09-05 00:00:00 | 2018-09-06 00:00:00 | 1 days 0 hours 0 minutes | 2 days 0 hours              |
|  3 | 2018-09-06 00:00:00 | 2018-09-08 00:00:00 | 2 days 0 hours 0 minutes | 0 days 0 hours              |
+----+---------------------+---------------------+--------------------------+-----------------------------+

列a和b通过窗体添加到表中。列c和“已用…”是根据上一行和当前行的值进行的计算。
当我尝试将表数据从php/mysql输出到html时,我得到的是:

+----+---------------------+---------------------+--------------------------+-----------------------------+
| ID |          A          |          B          |            C             | Elapsed since last checkout |
+----+---------------------+---------------------+--------------------------+-----------------------------+
|  2 | 2018-09-05 00:00:00 | 2018-09-06 00:00:00 | 1 days 0 hours 0 minutes | 2 days 0 hours              |
|  3 | 2018-09-06 00:00:00 | 2018-09-08 00:00:00 | 2 days 0 hours 0 minutes | 0 days 0 hours              |
+----+---------------------+---------------------+--------------------------+-----------------------------+

我使用这个问题的逻辑:如何在mysql中获取下一个/上一个记录?要组成sql语句从当前行的前一行中获取值,但不幸的是第一行没有显示:

"SELECT * FROM (select * from bookings WHERE id < $id ORDER BY id DESC LIMIT 1) AS x ORDER BY id LIMIT 1";

如何在一条sql语句中显示数据库中的所有行并从当前行的前一行中选择mysql单元格?
如果你需要浏览的话,这是我的全部代码。我使用嵌套查询是因为我不知道如何做(我读到了 JOIN 以及 LEFT JOIN 但根据我的理解,只有当你使用不同的表格时,我才这样做。)

$sqlQuery = "SELECT * FROM bookings";
$result = mysqli_query($conn, $sqlQuery);
$resultCheck = mysqli_num_rows($result);
if ($resultCheck > 0) {
    while ($row = mysqli_fetch_assoc($result)) {
        $id = $row['id'];
        $out = $row['check_out'];
        $in = $row['check_in'];
        $sum = $row['sum'];

        $sqlQueryLastDate = "SELECT * FROM (select * from bookings WHERE id < $id ORDER BY id DESC LIMIT 1) AS x ORDER BY id LIMIT 1";
        $resultLastDate = mysqli_query($conn, $sqlQueryLastDate);
        $resultCheckLastDate = mysqli_num_rows($resultLastDate);
        if ($resultCheckLastDate > 0) {
            while ($rowLastDate = mysqli_fetch_assoc($resultLastDate)) {
                $lastInDate = $rowLastDate['check_in'];
                //echo "previous row's in date:" .$lastInDate;

                $sqlQueryCurrentDate = "SELECT * FROM (select * from bookings WHERE id = $id ORDER BY id DESC LIMIT 1) AS x ORDER BY id LIMIT 1";
                $resultCurrentDate = mysqli_query($conn, $sqlQueryCurrentDate);
                $resultCheckCurrentDate = mysqli_num_rows($resultCurrentDate);
                if ($resultCheckCurrentDate > 0) {
                    while ($rowCurrentDate = mysqli_fetch_assoc($resultCurrentDate)) {
                        $currentOutDate = $rowCurrentDate['check_out'];
                        //echo "current row's out date:" .$currentOutDate;

                        $lastIn = new DateTime($lastInDate);
                        $currentOut = new DateTime($currentOutDate);
                        $intervalLastCurrent = $lastIn->diff($currentOut);
                        $elapsedLastCurrent = $intervalLastCurrent->format('%a days %h hours');
                        echo "
                                <tr>    
                                    <td>".$id."</td>
                                    <td>".$out."</td>
                                    <td>".$in."</td>
                                    <td>".$sum."</td>
                                    <td>".$elapsedLastCurrent."</td> 
                                </tr>
                            ";
                    } /*$sqlQueryCurrentDate*/
                }
            } /*$sqlQueryLastDate*/
        }
    } /*$sqlQuery*/
}

?>

即使你能为我提供必要的术语,让这项工作,我会做我自己的研究。例如,有人建议我使用 LEFT JOIN 但在调查后发现,这并不是我想要在这里实现的理想。
我试图实现但无法实现的事情:
如何减去sql中的前一行?
来自同一列的上一行
(及更多)

wfveoks0

wfveoks01#

解决这个问题的正确方法是使用仅在mysql 8中可用的窗口函数
如果没有窗口函数,则可以使用以下查询:

SELECT 
  *,
  (SELECT value FROM bookings WHERE id < b.id ORDER BY id LIMIT 1) AS prev 
FROM 
  bookings b 
ORDER BY id;

相关问题