MySQL 如何查詢更新時間在幾小時之內的資料

2021-10-09 15:33:45 字數 939 閱讀 7415

mysql 資料庫表 tbl_mgm_test 中有乙個字段 rec_upd_ts(記錄更新時間),資料型別為 timestamp,非空約束為 not null, 預設值為 default current_timestamp on update current_timestamp。

如題,如何查詢更新時間在 3 小時之內的資料?sql 語句如下所示。

select

*from tbl_mgm_test where rec_upd_ts >= date_sub(

now(),

interval

3hour);

select

*from tbl_mgm_test where timestampdiff(

second

, rec_upd_ts,

now())

<=3*

60*60;

select

*from tbl_mgm_test where rec_upd_ts >= date_add(

now(),

interval-3

hour

);

備註:如下語句,不能精確查出距離現在時間在 3 小時之內的資料,只能查出時間單位小時和現在的小時數相差在 3 以內的資料,查詢的資料報括 3 小時以內的資料和超出 3 小時但是小於 4 小時的資料(如當前時間為 2020-09-21 10:58:20,下方語句,不能精確查出 2020-09-21 07:58:20 之後的資料,查出的資料為 2020-09-21 06:58:20 之後的資料)。

select

*from tbl_mgm_test where timestampdiff(

hour

, rec_upd_ts,

now())

<=

3;

查詢mysql最後更新時間

檢視資料表是否有更新時,可以通過查詢information schema資料庫中的tables表中的有關所有的庫表資訊資料 information schema 資料庫跟 performance schema 一樣,都是 mysql 自帶的資訊資料庫。其中 performance schema 用於...

mysql更新時間 Mysql 更新時間

mysql時間加減函式為date add date sub 定義和用法 date add 函式向日期新增指定的時間間隔。date sub 函式向日期減少指定的時間間隔。語法date add date,interval expr type date sub date,interval expr typ...

mysql時間更新

mysql時間加減函式為date add date sub 定義和用法 date add 函式向日期新增指定的時間間隔。date sub 函式向日期減少指定的時間間隔。語法 date add date,interval expr type date sub date,interval expr ty...