

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Return a list from different rows into a single field with MySQL
For this, use GROUP_CONCAT(). Let us first create a table −
mysql> create table DemoTable2024 -> ( -> SubjectName varchar(20), -> StudentName varchar(20) -> ); Query OK, 0 rows affected (0.62 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable2024 values('MySQL','Chris'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable2024 values('MySQL','David'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable2024 values('MongoDB','Bob'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable2024 values('Java','Sam'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable2024 values('MongoDB','Mike'); Query OK, 1 row affected (0.17 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable2024;
This will produce the following output −
+-------------+-------------+ | SubjectName | StudentName | +-------------+-------------+ | MySQL | Chris | | MySQL | David | | MongoDB | Bob | | Java | Sam | | MongoDB | Mike | +-------------+-------------+ 5 rows in set (0.00 sec)
Following is the query to return a list from different rows into a single field −
mysql> select SubjectName,group_concat(StudentName) -> from DemoTable2024 -> group by SubjectName;
This will produce the following output −
+-------------+---------------------------+ | SubjectName | group_concat(StudentName) | +-------------+---------------------------+ | Java | Sam | | MongoDB | Bob,Mike | | MySQL | Chris,David | +-------------+---------------------------+ 3 rows in set (0.06 sec)
- Related Questions & Answers
- Return only a single row from duplicate rows with MySQL
- How to collapse rows into a comma-delimited list with a single MySQL Query?
- Coalesce values from different properties into a single array with MongoDB aggregation
- MySQL select distinct rows into a comma delimited list column?
- Combining multiple rows into a comma delimited list in MySQL?
- Count and sort rows with a single MySQL query
- Selecting only a single field from MongoDB?
- Add a single day to datetime field with MySQL INTERVAL
- Count multiple rows and display the result in different columns (and a single row) with MySQL
- Combine columns into rows with MySQL?
- Specify all the list properties into a single expression with CSS
- Inserting multiple parameter values into a single column with MySQL?
- Fetch random rows from a table with MySQL
- A single MySQL query to combine strings from many rows into a single row and display the corresponding User Id sum in another column?
- Concatenate multiple rows and columns in a single row with MySQL
Advertisements