使用 MySQL Select 获取时间戳日期范围?
要选择时间戳数据范围,请使用以下语法-
SELECT *FROM yourTableName where yourDataTimeField >= anyDateRange and yourDataTimeField < anyDateRange
为了理解上述语法,让我们创建一个表。创建表的查询如下-
mysql> create table DateRange −> ( −> DueTime timestamp −> );
使用插入命令在表中插入一些记录。查询如下-
mysql> insert into DateRange values('2016-11-13'); mysql> insert into DateRange values('2016-10-14'); mysql> insert into DateRange values('2017-01-23'); mysql> insert into DateRange values('2017-05-14'); mysql> insert into DateRange values('2017-08-25'); mysql> insert into DateRange values('2018-09-28'); mysql> insert into DateRange values('2018-11-17'); mysql> insert into DateRange values('2018-12-13'); mysql> insert into DateRange values('2018-12-16');
使用select命令显示表中的所有记录。查询如下-
mysql> select *from DateRange;
以下是输出-
+---------------------+ | DueTime | +---------------------+ | 2016-11-13 00:00:00 | | 2016-10-14 00:00:00 | | 2017-01-23 00:00:00 | | 2017-05-14 00:00:00 | | 2017-08-25 00:00:00 | | 2018-09-28 00:00:00 | | 2018-11-17 00:00:00 | | 2018-12-13 00:00:00 | | 2018-12-16 00:00:00 | +---------------------+ 9 rows in set (0.00 sec)
要选择时间戳日期范围,请使用以下查询-
mysql> select *from DateRange −> where DueTime >= '2017-05-14' −> and DueTime < '2018-12-17';
以下是输出-
+---------------------+ | DueTime | +---------------------+ | 2017-05-14 00:00:00 | | 2017-08-25 00:00:00 | | 2018-09-28 00:00:00 | | 2018-11-17 00:00:00 | | 2018-12-13 00:00:00 | | 2018-12-16 00:00:00 | +---------------------+ 6 rows in set (0.00 sec)
假设您的时间戳是unix时间戳,则使用以下语法。
select *from yourTableName where yourColumnName >= unix_timestamp('anyDateValue’) and yourColumnName < unix_timestamp('anyDateValue’)