- 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
How to group by date regardless of time in MySQL?
When you have identical dates in a table with different time values for each, you can group them easily with GROUP BY DATE.
Let us first create a table -
mysql> create table DemoTable692 (DueDatetime datetime); Query OK, 0 rows affected (0.97 sec)
Insert some records in the table using insert command:
mysql> insert into DemoTable692 values('2019-07-21 10:20:00'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable692 values('2019-06-11 11:00:10'); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable692 values('2019-07-21 11:00:10'); Query OK, 1 row affected (1.97 sec) mysql> insert into DemoTable692 values('2019-07-21 12:10:10'); Query OK, 1 row affected (0.18 sec)
Display all records from the table using select statement:
mysql> select *from DemoTable692;
This will produce the following output -
+---------------------+ | DueDatetime | +---------------------+ | 2019-07-21 10:20:00 | | 2019-06-11 11:00:10 | | 2019-07-21 11:00:10 | | 2019-07-21 12:10:10 | +---------------------+ 4 rows in set (0.00 sec)
Here is the query to GROUP BY DATE regardless of time -
mysql> select *from DemoTable692 GROUP BY DATE(DueDatetime);
This will produce the following output -
+---------------------+ | DueDatetime | +---------------------+ | 2019-07-21 10:20:00 | | 2019-06-11 11:00:10 | +---------------------+ 2 rows in set (0.00 sec)
Advertisements