To select timestamp data range, use the below syntax −
SELECT *FROM yourTableName where yourDataTimeField >= anyDateRange and yourDataTimeField < anyDateRange
To understand the above syntax, let us create a table. The query to create a table is as follows −
mysql> create table DateRange −> ( −> DueTime timestamp −> ); Query OK, 0 rows affected (1.34 sec)
Insert some records in the table using insert command. The query is as follows −
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)
Display all records from the table using select command. The query is as follows −
mysql> select *from DateRange;
The following is the output −
+---------------------+ | 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)
To select timestamp date range, use the following query −
mysql> select *from DateRange −> where DueTime >= '2017-05-14' −> and DueTime < '2018-12-17';
The following is the output −
+---------------------+ | 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)
Suppose if your timestamp is in unix timestamp, then use the following syntax.
select *from yourTableName where yourColumnName >= unix_timestamp('anyDateValue’) and yourColumnName < unix_timestamp('anyDateValue’)