
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
MySQL Query to Count Dates and Fetch Repeated Dates
To display the count, use aggregate function COUNT(*). Let us first create a table −
mysql> create table DemoTable1321 -> ( -> ArrivalDatetime timestamp -> ); Query OK, 0 rows affected (0.50 sec)
Example
Insert some records in the table using insert command −
mysql> insert into DemoTable1321 values(now()); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1321 values('2019-01-10 12:34:00'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1321 values('2019-06-12 11:34:00'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1321 values('2019-06-12 04:50:00'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1321 values('2019-09-18 10:50:45'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable1321 values('2019-06-12 06:10:20'); Query OK, 1 row affected (0.11 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1321;
Output
+---------------------+ | ArrivalDatetime | +---------------------+ | 2019-09-18 21:49:17 | | 2019-01-10 12:34:00 | | 2019-06-12 11:34:00 | | 2019-06-12 04:50:00 | | 2019-09-18 10:50:45 | | 2019-06-12 06:10:20 | +---------------------+ 6 rows in set (0.00 sec)
Example
Following is the query to count the dates −
mysql> select date(ArrivalDatetime) as OnlyDate,count(*) as Total from DemoTable1321 -> group by date(ArrivalDatetime) -> order by date(ArrivalDatetime);
Output
+------------+-------+ | OnlyDate | Total | +------------+-------+ | 2019-01-10 | 1 | | 2019-06-12 | 3 | | 2019-09-18 | 2 | +------------+-------+ 3 rows in set (0.00 sec)
Advertisements