

- 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
MySQL query to list all the items in a group in one record?
You can use GROUP_CONCAT() function to list all the items in a group in one record. Let us first create a table −
mysql> create table DemoTable ( ProductId int, ProductName varchar(40), ProductCategory varchar(40) ); Query OK, 0 rows affected (0.67 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(100,'Product-1','1Product'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(101,'Product-2','2Product'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(100,'Product-1','3Product'); Query OK, 1 row affected (0.14 sec)
Following is the query to display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-----------+-------------+-----------------+ | ProductId | ProductName | ProductCategory | +-----------+-------------+-----------------+ | 100 | Product-1 | 1Product | | 101 | Product-2 | 2Product | | 100 | Product-1 | 3Product | +-----------+-------------+-----------------+ 3 rows in set (0.00 sec)
Following is the query to list all the items in a group in a single record −
mysql> select ProductId,group_concat(ProductCategory) AS `GROUP` FROM DemoTable group by ProductId;
This will produce the following output −
+-----------+-------------------+ | ProductId | GROUP | +-----------+-------------------+ | 100 | 1Product,3Product | | 101 | 2Product | +-----------+-------------------+ 2 rows in set (0.00 sec)
- Related Questions & Answers
- MySQL query to return all items in a single row
- Link Bootstrap List Group Items
- How to query all items in MongoDB?
- MySQL query to display record with maximum count values in a group with other column values?
- MySQL query to delete a record with the lowest ID?
- MySQL query to place a specific record on the top
- How to list all users in a Linux group?
- MySQL - How to count all rows per table in one query?
- Sort and Group in one MongoDB aggregation query?
- How to return the nth record from MySQL query?
- MySQL query to search record with apostrophe?
- MySQL query to fetch record by year
- MySQL query to decrease the value of a specific record to zero?
- Can we GROUP BY one column and select all data in MySQL?
- Counting different distinct items in a single MySQL query?
Advertisements