
- 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 group concat distinct by Id?
Let us first create a table −
mysql> create table DemoTable ( Id int, Name varchar(100) ); Query OK, 0 rows affected (0.64 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(100,'Chris'); Query OK, 1 row affected (0.44 sec) mysql> insert into DemoTable values(200,'Robert'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(100,'Chris'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(300,'David'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(400,'Mike'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(400,'Mike'); Query OK, 1 row affected (0.10 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+------+--------+ | Id | Name | +------+--------+ | 100 | Chris | | 200 | Robert | | 100 | Chris | | 300 | David | | 400 | Mike | | 400 | Mike | +------+--------+ 6 rows in set (0.00 sec)
Following is the query to MySQL group Concat distinct by Id −
mysql> select group_concat(Name) from ( select distinct Id,Name from DemoTable ) tbl;
This will produce the following output −
+-------------------------+ | group_concat(Name) | +-------------------------+ | Chris,Robert,David,Mike | +-------------------------+ 1 row in set (0.00 sec)
- Related Articles
- MySQL query to select distinct order by id
- MySQL GROUP BY and CONCAT() to display distinct first and last name
- SELECT DISTINCT vs GROUP BY in MySQL?
- MySQL query to GROUP BY multiple columns
- MySQL group by for separate id without using GROUP BY to remove duplicate column row?
- MySQL select * with distinct id?
- Getting last value in MySQL group concat?
- How Can MySQL GROUP BY clause behave like DISTINCT clause?
- How to create JSON format with group-concat in MySQL?
- MySQL query to find all rows where ID is divisible by 4?
- How to ORDER BY FIELD with GROUP BY in a single MySQL query?
- MongoDB query to group by _id
- Can we use “LIKE concat()” in a MySQL query?
- MySQL concat() to create column names to be used in a query?
- Display the record with non-duplicate Id using MySQL GROUP BY and HAVING

Advertisements