
- 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
GROUP BY the number of rows returned by GROUP BY in MySQL?
You can use GROUP_CONCAT() for this. To understand the above concept, let us create a table.
The query to create a table is as follows
mysql> create table groupByDemo -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Name varchar(100) -> ); Query OK, 0 rows affected (1.31 sec)
Insert some records in the table using insert command.
The query is as follows
mysql> insert into groupByDemo(Name) values('John'); Query OK, 1 row affected (0.19 sec) mysql> insert into groupByDemo(Name) values('Carol'); Query OK, 1 row affected (0.14 sec) mysql> insert into groupByDemo(Name) values('Carol'); Query OK, 1 row affected (0.10 sec) mysql> insert into groupByDemo(Name) values('Bob'); Query OK, 1 row affected (0.24 sec) mysql> insert into groupByDemo(Name) values('Bob'); Query OK, 1 row affected (0.23 sec) mysql> insert into groupByDemo(Name) values('Bob'); Query OK, 1 row affected (0.17 sec) mysql> insert into groupByDemo(Name) values('John'); Query OK, 1 row affected (0.16 sec) mysql> insert into groupByDemo(Name) values('John'); Query OK, 1 row affected (0.07 sec) mysql> insert into groupByDemo(Name) values('John'); Query OK, 1 row affected (0.11 sec) mysql> insert into groupByDemo(Name) values('Sam'); Query OK, 1 row affected (0.17 sec) mysql> insert into groupByDemo(Name) values('Carol'); Query OK, 1 row affected (0.19 sec)
Display all records from the table using select statement.
The query is as follows
mysql> select *from groupByDemo;
The following is the output
+----+-------+ | Id | Name | +----+-------+ | 1 | John | | 2 | Carol | | 3 | Carol | | 4 | Bob | | 5 | Bob | | 6 | Bob | | 7 | John | | 8 | John | | 9 | John | | 10 | Sam | | 11 | Carol | +----+-------+ 11 rows in set (0.00 sec)
Here is the query to group by number of rows
mysql> SELECT Counter, GROUP_CONCAT(Name SEPARATOR ', ') as AllName -> FROM (SELECT Name, COUNT(Name) as Counter -> FROM groupByDemo -> GROUP BY Name) tbl -> GROUP BY Counter -> ORDER BY Counter DESC;
The following is the output
+---------+------------+ | Counter | AllName | +---------+------------+ | 4 | John | | 3 | Carol, Bob | | 1 | Sam | +---------+------------+ 3 rows in set (0.00 sec)
- Related Articles
- Listing all rows by group with MySQL GROUP BY?
- Get rows with GROUP BY in MySQL?
- Group MySQL rows in an array by column value?
- How to order or choose rows in MySQL GROUP BY clause?
- HAVING with GROUP BY in MySQL
- How can we apply filtering criteria at group levels of the result set returned by MySQL?
- MySQL “order by” inside of “group by”? Is it possible?
- MySQL query to group rows by the numeric characters in a string field?
- MySQL: update field with Group By?
- MySQL- GROUP and COUNT by date?
- Limit the count using GROUP BY in MySQL
- SELECT DISTINCT vs GROUP BY in MySQL?
- MySQL group by for separate id without using GROUP BY to remove duplicate column row?
- MySQL query to GROUP BY multiple columns
- MySQL GROUP BY date when using datetime?

Advertisements