Get two days data (today and yesterday) from a MySQL table with timestamp values

MySQLMySQLi Database

Let us first create a table −

mysql> create table DemoTable1495
   -> (
   -> ShippingDate bigint
   -> );
Query OK, 0 rows affected (0.63 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1495 values(1570127400);
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable1495 values(1570213800);
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable1495 values(1570645800);
Query OK, 1 row affected (0.19 sec)
mysql> insert into DemoTable1495 values(1570300200);
Query OK, 1 row affected (0.15 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1495;

This will produce the following output −

+--------------+
| ShippingDate |
+--------------+
|   1570127400 |
|   1570213800 |
|   1570645800 |
|   1570300200 |
+--------------+
4 rows in set (0.00 sec)

The current date is as follows −

mysql> select curdate();
+------------+
| curdate()  |
+------------+
| 2019-10-06 |
+------------+
1 row in set (0.00 sec)

Here is the query to get two days data from MySQL −

mysql> select * from DemoTable1495
   -> where ShippingDate >=unix_timestamp(curdate() - interval 1 day)
   -> and
   -> ShippingDate < unix_timestamp(curdate() + interval 1 day);

This will produce the following output −

+--------------+
| ShippingDate |
+--------------+
|   1570213800 |
|   1570300200 |
+--------------+
2 rows in set (0.00 sec)
raja
Published on 11-Dec-2019 10:07:20
Advertisements