Find the number of logins between two dates in MySQL

MySQLMySQLi Database

Use BETWEEN to find the logins between two dates. Let us first create a table −

mysql> create table DemoTable
(
   Login datetime
);
Query OK, 0 rows affected (0.66 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('2019-08-10');
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable values('2019-08-12');
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable values('2019-08-20');
Query OK, 1 row affected (0.09 sec)
mysql> insert into DemoTable values('2019-08-24');
Query OK, 1 row affected (0.12 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+---------------------+
| Login               |
+---------------------+
| 2019-08-10 00:00:00 |
| 2019-08-12 00:00:00 |
| 2019-08-20 00:00:00 |
| 2019-08-24 00:00:00 |
+---------------------+
4 rows in set (0.00 sec)

Following is the correct query for MySQL date between −

mysql> select count(*) from DemoTable where Login between '2019-08-12' and '2019-08-24';

This will produce the following output −

+----------+
| count(*) |
+----------+
|        3 |
+----------+
1 row in set (0.05 sec)
raja
Published on 30-Sep-2019 11:28:26
Advertisements