
- 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 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 Articles
- MySQL query to return all items in a single row
- MySQL query to display record with maximum count values in a group with other column values?
- How to query all items in MongoDB?
- Link Bootstrap List Group Items
- MySQL query to delete a record with the lowest ID?
- MySQL query to place a specific record on the top
- MySQL - How to count all rows per table in one query?
- MySQL query to search record with apostrophe?
- MySQL query to fetch record by year
- How to list all users in a Linux group?
- How to return the nth record from MySQL query?
- How to get a specific column record from SELECT query in MySQL?
- MySQL query to decrease the value of a specific record to zero?
- Counting different distinct items in a single MySQL query?
- How to find the previous and next record using a single query in MySQL?

Advertisements