您可以使用now()
MySQL的date-sub()和函数来获取上一小时添加的行。
语法如下-
select *from yourTableName where yourDateTimeColumnName <=date_sub(now(),interval 1 hour);
上面的查询给出了最后一个小时添加的结果。为了理解上述概念,让我们首先创建一个表。创建表的查询如下-
mysql> create table LastHourRecords -> ( -> Id int, -> Name varchar(100), -> Login datetime -> );
使用insert命令以datetime形式插入记录。插入记录的查询如下-
mysql> insert into LastHourRecords values(1,'John',' 2018-12-19 10:00:00'); mysql> insert into LastHourRecords values(2,'Carol','2018-12-19 10:10:00'); mysql> insert into LastHourRecords values(3,'Sam','2018-12-19 10:05:00'); mysql> insert into LastHourRecords values(4,'Mike','2018-12-18 12:10:00');
使用select语句显示表中的所有记录。查询如下-
mysql> select *from LastHourRecords;
输出结果
+------+-------+---------------------+ | Id | Name | Login | +------+-------+---------------------+ | 1 | John | 2018-12-19 10:00:00 | | 2 | Carol | 2018-12-19 10:10:00 | | 3 | Sam | 2018-12-19 10:05:00 | | 4 | Mike | 2018-12-18 12:10:00 | +------+-------+---------------------+ 4 rows in set (0.00 sec)
让我们看一下查询以获取最近一小时添加的行-
mysql> select *from LastHourRecords -> where Login <=Date_sub(now(),interval 1 hour);
输出结果
+------+-------+---------------------+ | Id | Name | Login | +------+-------+---------------------+ | 1 | John | 2018-12-19 10:00:00 | | 2 | Carol | 2018-12-19 10:10:00 | | 3 | Sam | 2018-12-19 10:05:00 | +------+-------+---------------------+ 3 rows in set (0.00 sec)