
- 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
Display records by grouping dates in MySQL
To group dates in MySQL, use the GROUP BY clause −
mysql> create table DemoTable2002 ( CustomerName varchar(20), CustomerShippingDate datetime ); Query OK, 0 rows affected (0.61 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable2002 values('Chris','2019-01-10'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable2002 values('David','2018-12-31'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable2002 values('David','2019-12-16'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable2002 values('Chris','2018-12-01'); Query OK, 1 row affected (0.12 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable2002;
This will produce the following output −
+--------------+----------------------+ | CustomerName | CustomerShippingDate | +--------------+----------------------+ | Chris | 2019-01-10 00:00:00 | | David | 2018-12-31 00:00:00 | | David | 2019-12-16 00:00:00 | | Chris | 2018-12-01 00:00:00 | +--------------+----------------------+ 4 rows in set (0.00 sec)
Here is the query to display records by grouping dates −
mysql> select date(min(CustomerShippingDate)),date(max(CustomerShippingDate)),CustomerName from DemoTable2002 group by CustomerName;
This will produce the following output −
+---------------------------------+---------------------------------+--------------+ | date(min(CustomerShippingDate)) | date(max(CustomerShippingDate)) | CustomerName | +---------------------------------+---------------------------------+--------------+ | 2018-12-01 | 2019-01-10 | Chris | | 2018-12-31 | 2019-12-16 | David | +---------------------------------+---------------------------------+--------------+ 2 rows in set (0.00 sec)
- Related Articles
- Display distinct dates in MySQL from a column with date records
- Change only dates, ignoring time records in MySQL
- MySQL query to display records ordered by numeric difference?
- Select the date records between two dates in MySQL
- Display records ignoring NULL in MySQL
- Display MongoDB records by ObjectId?
- MySQL query to display records ordered by DESC while skipping some?
- Find and display duplicate records in MySQL?
- Display all records except one in MySQL
- How to ORDER BY DESC and display the first 3 records in MySQL?
- Get the SUM of records between two given dates in MySQL
- Display records after a particular date in MySQL
- MySQL query for grouping and summing the values based on specific records
- Filter dates from a table with DATE and NULL records in MySQL
- Display and concatenate records ignoring NULL values in MySQL

Advertisements