How to group by date regardless of time in MySQL?

MySQLMySQLi Database

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)
raja
Published on 21-Aug-2019 11:33:06
Advertisements