How does MySQL query data whose update time is within a few hours

  The MySQL database table tbl_mgm_test has a field rec_upd_ts (record update time), the data type is timestamp, the non-null constraint is NOT NULL, and the default value is DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP.

  Like the title, how to query the data updated within 3 hours? The SQL statement is as follows.

SELECT * FROM tbl_mgm_test WHERE rec_upd_ts >= DATE_SUB(NOW(), INTERVAL 3 HOUR);

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);

  Note: The following sentence cannot accurately find out the data within 3 hours from the current time, but only find out the data within 3 hours of the time unit hour and the current hour. The queried data includes data within 3 hours and beyond 3 hours but less than 4 hours of data (for example, the current time is 2020-09-21 10:58:20, the sentence below cannot accurately find the data after 2020-09-21 07:58:20, the data found is Data after 2020-09-21 06:58:20).

SELECT * FROM tbl_mgm_test WHERE TIMESTAMPDIFF(HOUR , rec_upd_ts, NOW()) <= 3;

Guess you like

Origin blog.csdn.net/piaoranyuji/article/details/108704274