- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find the number of logins between two dates in MySQL
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)
Advertisements