

- 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
How to concatenate strings using both GROUP_CONCAT() and CONCAT() in the same MySQL query?
The CONCAT() method is used to concat, whereas GROUP_CONCAT() is used to concatenate strings from a group in a single string.
Let us first create a table −
mysql> create table DemoTable799 ( UserId int, UserName varchar(100), UserAge int ); Query OK, 0 rows affected (0.56 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable799 values(101,'John',21); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable799 values(102,'Chris',26); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable799 values(101,'Robert',23); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable799 values(103,'David',24); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable799 values(101,'Mike',29); Query OK, 1 row affected (0.18 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable799;
This will produce the following output -
+--------+----------+---------+ | UserId | UserName | UserAge | +--------+----------+---------+ | 101 | John | 21 | | 102 | Chris | 26 | | 101 | Robert | 23 | | 103 | David | 24 | | 101 | Mike | 29 | +--------+----------+---------+ 5 rows in set (0.00 sec)
Here is the query to concatenate strings with CONCAT() and GROUP_CONCAT() −
mysql> select UserId,GROUP_CONCAT(CONCAT('MR.', UserName)) from DemoTable799 group by UserId;
This will produce the following output -
+--------+---------------------------------------+ | UserId | GROUP_CONCAT(CONCAT('MR.', UserName)) | +--------+---------------------------------------+ | 101 | MR.John,MR.Robert,MR.Mike | | 102 | MR.Chris | | 103 | MR.David | +--------+---------------------------------------+ 3 rows in set (0.00 sec)
- Related Questions & Answers
- MySQL query to group concat distinct by Id?
- Getting last value in MySQL group concat?
- How to create JSON format with group-concat in MySQL?
- concat(), replace(), and trim() Strings in Java.
- MySQL GROUP BY and CONCAT() to display distinct first and last name
- Use MySQL concat() and lower() effectively
- How to concat Values in MySQL Query and to handle Null values as well?
- Perform complex MySQL insert by using CONCAT()?
- How to use GROUP_CONCAT in CONCAT in MySQL?
- Concat a string to SELECT * in MySQL?
- MySQL concat() to create column names to be used in a query?
- Concat a field in MySQL SELECT?
- MySQL query to group concat and place data into a single row on the basis of 1 values in corresponding column?
- Add to existing value in MySQL column using CONCAT function?
- Can we use “LIKE concat()” in a MySQL query?
Advertisements