- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Listing all rows by group with MySQL GROUP BY?
To list all rows by group, you can use GROUP_CONCAT(). Let us first create a table −
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Name varchar(20), Value varchar(100) ); Query OK, 0 rows affected (0.62 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(Name,Value) values('John','John'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(Name,Value) values('Carol','Carol'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable(Name,Value) values('John','Works'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(Name,Value) values('Carol','Works'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable(Name,Value) values('John','At'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(Name,Value) values('Carol','At'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(Name,Value) values('John','Amazon'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(Name,Value) values('Carol','Google'); Query OK, 1 row affected (0.16 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output -
+----+-------+--------+ | Id | Name | Value | +----+-------+--------+ | 1 | John | John | | 2 | Carol | Carol | | 3 | John | Works | | 4 | Carol | Works | | 5 | John | At | | 6 | Carol | At | | 7 | John | Amazon | | 8 | Carol | Google | +----+-------+--------+ 8 rows in set (0.00 sec)
Following is the query to list all rows by group. Here value for ‘John’ would be fetched i.e. “John Works At Amazon from id 1, 3, 5, 7. In the same way, it would work for ‘Carol’ −
mysql> select Name, GROUP_CONCAT(Value SEPARATOR ' ') AS `Complete_Status` from DemoTable group by Name;
This will produce the following output −
+-------+-----------------------+ | Name | Complete_Status | +-------+-----------------------+ | Carol | Carol Works At Google | | John | John Works At Amazon | +-------+-----------------------+ 2 rows in set (0.00 sec)
- Related Articles
- Get rows with GROUP BY in MySQL?
- GROUP BY the number of rows returned by GROUP BY in MySQL?
- Group MySQL rows in an array by column value?
- HAVING with GROUP BY in MySQL
- MySQL: update field with Group By?
- How to order or choose rows in MySQL GROUP BY clause?
- MySQL- GROUP and COUNT by date?
- MySQL query to group rows by the numeric characters in a string field?
- Why should we not use group functions with non-group fields without GROUP BY clause in MySQL SELECT query?
- MySQL group by for separate id without using GROUP BY to remove duplicate column row?
- How to ORDER BY FIELD with GROUP BY in a single MySQL query?
- Can we GROUP BY one column and select all data in MySQL?
- SELECT DISTINCT vs GROUP BY in MySQL?
- MySQL query to GROUP BY multiple columns
- MySQL GROUP BY date when using datetime?

Advertisements