- 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
How to use GROUP_CONCAT in CONCAT in MySQL?
Firstly, let us create a table.
mysql> create table GroupConcatenateDemo -> ( -> id int, -> FirstName varchar(100), -> Score int -> ); Query OK, 0 rows affected (0.61 sec)
Inserting records
Let us now insert some records.
mysql> insert into GroupConcatenateDemo values(1,'John',94); Query OK, 1 row affected (0.16 sec) mysql> insert into GroupConcatenateDemo values(2,'Bob',98); Query OK, 1 row affected (0.16 sec) mysql> insert into GroupConcatenateDemo values(4,'Carol',100); Query OK, 1 row affected (0.20 sec)
To check how many records are present in the table.
mysql> select *from GroupConcatenateDemo;
Here is the output.
+------+-----------+-------+ | id | FirstName | Score | +------+-----------+-------+ | 1 | John | 94 | | 2 | Bob | 98 | | 3 | Carol | 100 | +------+-----------+-------+ 3 rows in set (0.00 sec)
Syntax to use GROUP_CONCAT
The following is the syntax to concatenate records.
Select column_name1,group_concat(concat(`Column_name2`,'separatorValue',`Column_name3`) separator 'separatorValue') as AliasName yourTableName group by column_name1;
The following is an example.
mysql> SELECT -> id,group_concat(concat(`FirstName`,':',`score`) separator ',') -> as FirstNameAndScore from GroupConcatenateDemo group by id;
Here is the output that shows we have successfully concatenated records.
+------+-------------------+ | id | FirstNameAndScore | +------+-------------------+ | 1 | John:94 | | 2 | Bob:98 | | 3 | Carol:100 | +------+-------------------+ 3 rows in set (0.00 sec)
- Related Articles
- How to concatenate strings using both GROUP_CONCAT() and CONCAT() in the same MySQL query?
- How to quote values of single column using GROUP_CONCAT and CONCAT with DISTINCT in MySQL?
- Use MySQL concat() and lower() effectively
- How to quote values using MySQL group_concat?
- Implement numbering in MySQL GROUP_CONCAT
- Can we use “LIKE concat()” in a MySQL query?
- New line separator doesn't work for group_concat function in MySQL? How to use it correctly?
- Using GROUP_CONCAT() on bit fields returns garbage in MySQL? How to fix?
- Concat a string to SELECT * in MySQL?
- How to create JSON format with group-concat in MySQL?
- Concatenate data from multiple rows using GROUP_CONCAT() in MySQL?
- Concat a field in MySQL SELECT?
- MySQL group_concat to add a separator for empty fields?
- Getting last value in MySQL group concat?
- How to concat Values in MySQL Query and to handle Null values as well?

Advertisements