
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
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)
- Related Articles
- How to order by date and time in MySQL?
- MySQL- GROUP and COUNT by date?
- MySQL GROUP BY date when using datetime?
- How to set default date time as system date time in MySQL?
- How to insert current date/time in MySQL?
- Sort by date & time in descending order in MySQL?
- MySQL query to group results by date and display the count of duplicate values?
- How to implement GROUP by range in MySQL?
- How to convert JS date time to MySQL datetime?
- Changing data type from date to date/time in MySQL?
- How to part DATE and TIME from DATETIME in MySQL?
- Increment date/time value by second with MySQL query?
- GROUP BY the number of rows returned by GROUP BY in MySQL?
- How to perform conditional GROUP BY in MySQL to fetch?
- How to determine if a value appears in a GROUP BY group in MySQL?

Advertisements