Fastest way to search for a date from the date records in MySQL

MySQLMySQLi Database

The fastest and easiest way is to use the MySQL BETWEEN keyword. Let us first create a −

mysql> create table DemoTable1413
   -> (
   -> EmployeeName varchar(20),
   -> EmployeeJoiningDate datetime
   -> );
Query OK, 0 rows affected (0.45 sec)

Insert some records in the table using insert −

mysql> insert into DemoTable1413 values('Chris','2018-09-28 11 :10 :50');
Query OK, 1 row affected (0.08 sec)
mysql> insert into DemoTable1413 values('David','2019-09-28 11:10:50');
Query OK, 1 row affected (0.20 sec)
mysql> insert into DemoTable1413 values('Mike','2019-09-29 12:40:00');
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable1413 values('Carol','2019-09-28 12:06:10');
Query OK, 1 row affected (0.08 sec)

Display all records from the table using select −

mysql> select * from DemoTable1413;

This will produce the following output −

+--------------+---------------------+
| EmployeeName | EmployeeJoiningDate |
+--------------+---------------------+
| Chris        | 2018-09-28 11:10:50 |
| David        | 2019-09-28 11:10:50 |
| Mike         | 2019-09-29 12:40:00 |
| Carol        | 2019-09-28 12:06:10 |
+--------------+---------------------+
4 rows in set (0.00 sec)

Here is the query to search for date records between dates −

mysql> select * from DemoTable1413
   -> where EmployeeJoiningDate between '2019-09-28' and '2019-09-28 23:59:59'
   -> limit 1;

This will produce the following output −

+--------------+---------------------+
| EmployeeName | EmployeeJoiningDate |
+--------------+---------------------+
| David        | 2019-09-28 11:10:50 |
+--------------+---------------------+
1 row in set (0.03 sec)
raja
Published on 12-Nov-2019 05:14:34
Advertisements