用 MySQL Select 获取时间戳日期范围?
要选择时间戳数据范围,请使用以下语法 −
SELECT *FROM yourTableName where yourDataTimeField >= anyDateRange and yourDataTimeField < anyDateRange
为了理解上述语法,让我们创建一个表。创建表的查询如下 −
mysql> create table DateRange −> ( −> DueTime timestamp −> ); Query OK, 0 rows affected (1.34 sec)
使用 insert 命令向表中插入一些记录。查询如下 −
mysql> insert into DateRange values('2016-11-13'); Query OK, 1 row affected (0.51 sec) mysql> insert into DateRange values('2016-10-14'); Query OK, 1 row affected (0.23 sec) mysql> insert into DateRange values('2017-01-23'); Query OK, 1 row affected (0.10 sec) mysql> insert into DateRange values('2017-05-14'); Query OK, 1 row affected (0.19 sec) mysql> insert into DateRange values('2017-08-25'); Query OK, 1 row affected (0.17 sec) mysql> insert into DateRange values('2018-09-28'); Query OK, 1 row affected (0.18 sec) mysql> insert into DateRange values('2018-11-17'); Query OK, 1 row affected (0.47 sec) mysql> insert into DateRange values('2018-12-13'); Query OK, 1 row affected (0.17 sec) mysql> insert into DateRange values('2018-12-16'); Query OK, 1 row affected (0.27 sec)
使用 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’)
广告