簡體   English   中英

如何在sql中添加2個時間戳列,其中1是當前時間,另一個是當前時間+30分鍾?

[英]how to add 2 timestamp column in sql where in 1 is current time and the other is current time+30minutes?

+---------------------+---------------------+
| current time        | expiry time         |
+---------------------+---------------------+
| 10:10:10            | 10:30:10            |
| 23:40:10            | 00:10:10            |
| 13:10:10            | 13:40:10            |
| 05:10:00            | 05:40:00            |
|                     |                     |
|                     |                     |
+---------------------+---------------------+

表中還有另一列是 id 和 x 列。 基本上,當將值發送到 x 列時,會更新 2 時間戳。 一個是當前時間,另一個是當前時間,但 +30 分鍾

我正在使用 MariaDB,而 php 值來自傳感器。

if ($value == $row['end_at']){
    echo "XXX";
    $today = date("H:i:s");  
    $up = date("H:i:s", strtotime("+30 minutes"));
    $reg= "insert into table (current time+30mins) values ('$up')";
} else if  {

}

嘗試這樣做,但最終每秒都在不斷更新數據庫。

更新我想到的另一個想法是添加另一個具有不同時區的列時間戳,即當前時間:GMT +8 到期時間:GMT+:8:30 這是否合理? 我該怎么做?

您的插入內容應如下所示:

insert into t (x, id, current_time, expiry_time)
    values (?, ?, now(), now() + interval 30 minute);

更新應如下所示:

update t
    set x = ?,
       current_time = now(),
       expiry_time = now() + interval 30 minute
    where id = ?;

? 是用於將值傳遞給查詢的參數占位符。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM