
- 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
MySQL- GROUP and COUNT by date?
You can use GROUP BY clause and COUNT() function for this. The syntax is as follows −
SELECT yourColumnName1,yourColumnName2,..N,COUNT(*) as anyAliasName FROM yourTableName GROUP BY yourColumnName1,yourColumnName2;
To understand the above syntax, let us create a table. The query to create a table is as follows −
mysql> create table GroupAndCountByDate -> ( -> Id int NOT NULL AUTO_INCREMENT, -> TripDate date, -> ShopId int, -> PRIMARY KEY(Id) -> ); Query OK, 0 rows affected (0.79 sec)
Now you can insert some records in the table using insert command. The query is as follows −
mysql> insert into GroupAndCountByDate(TripDate,ShopId) values('2019-01-31',10); Query OK, 1 row affected (0.52 sec) mysql> insert into GroupAndCountByDate(TripDate,ShopId) values('2019-02-01',15); Query OK, 1 row affected (0.17 sec) mysql> insert into GroupAndCountByDate(TripDate,ShopId) values('2019-01-31',10); Query OK, 1 row affected (0.23 sec) mysql> insert into GroupAndCountByDate(TripDate,ShopId) values('2019-02-01',15); Query OK, 1 row affected (0.31 sec) mysql> insert into GroupAndCountByDate(TripDate,ShopId) values('2019-03-23',20); Query OK, 1 row affected (0.15 sec) mysql> insert into GroupAndCountByDate(TripDate,ShopId) values('2019-04-21',25); Query OK, 1 row affected (0.20 sec) mysql> insert into GroupAndCountByDate(TripDate,ShopId) values('2019-03-13',20); Query OK, 1 row affected (0.16 sec) mysql> insert into GroupAndCountByDate(TripDate,ShopId) values('2019-04-06',25); Query OK, 1 row affected (0.17 sec)
Display all records from the table using select statement. The query is as follows −
mysql> select *from GroupAndCountByDate;
The following is the output −
+----+------------+--------+ | Id | TripDate | ShopId | +----+------------+--------+ | 1 | 2019-01-31 | 10 | | 2 | 2019-02-01 | 15 | | 3 | 2019-01-31 | 10 | | 4 | 2019-02-01 | 15 | | 5 | 2019-03-23 | 20 | | 6 | 2019-04-21 | 25 | | 7 | 2019-03-13 | 20 | | 8 | 2019-04-06 | 25 | +----+------------+--------+ 8 rows in set (0.00 sec)
Here is the query to GROUP and count by date −
mysql> select TripDate,ShopId,COUNT(*) as TOTALTRIP -> from GroupAndCountByDate -> group by TripDate,ShopId;
The following is the output −
+------------+--------+-----------+ | TripDate | ShopId | TOTALTRIP | +------------+--------+-----------+ | 2019-01-31 | 10 | 2 | | 2019-02-01 | 15 | 2 | | 2019-03-23 | 20 | 1 | | 2019-04-21 | 25 | 1 | | 2019-03-13 | 20 | 1 | | 2019-04-06 | 25 | 1 | +------------+--------+-----------+ 6 rows in set (0.00 sec)
- Related Articles
- MySQL query to group results by date and display the count of duplicate values?
- MySQL GROUP BY date when using datetime?
- Using group by on two fields and count in MySQL?
- Limit the count using GROUP BY in MySQL
- MySQL GROUP BY with WHERE clause and condition count greater than 1?
- How to group by date regardless of time in MySQL?
- Using GROUP BY and COUNT in a single MySQL query to group duplicate records and display corresponding max value
- MongoDB SELECT COUNT GROUP BY?
- MySQL query to group by names and display the count in a new column
- Aggregation: group date in nested documents (nested object) and display the count?
- Listing all rows by group with MySQL GROUP BY?
- MySQL query to count days in date range with start and end date
- JavaScript group a JSON object by two properties and count
- Example of SQL query describing COUNT function and GROUP BY
- Get first date from timestamp in MySQL group by another column with duplicate value

Advertisements