
- 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 concatenate records with similar corresponding ids in a single row separated by a special character
For this, you can use CONCAT_WS() along with GROUP_CONCAT(). Let us first create a
mysql> create table DemoTable2016 -> ( -> UserId int, -> UserName varchar(20) -> ); Query OK, 0 rows affected (0.51 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable2016 values(1,'Chris'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable2016 values(2,'Bob'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable2016 values(1,'David'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable2016 values(2,'Carol'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable2016 values(1,'Sam'); Query OK, 1 row affected (0.14 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable2016;
This will produce the following output −
+--------+----------+ | UserId | UserName | +--------+----------+ | 1 | Chris | | 2 | Bob | | 1 | David | | 2 | Carol | | 1 | Sam | +--------+----------+ 5 rows in set (0.00 sec)
Here is the query to concatenate records in a row separated by a special character −
mysql> select group_concat(concat_ws('-',UserName,UserId)) from DemoTable2016 group by UserId;
This will produce the following output −
+----------------------------------------------+ | group_concat(concat_ws('-',UserName,UserId)) | +----------------------------------------------+ | Chris-1,David-1,Sam-1 | | Bob-2,Carol-2 | +----------------------------------------------+ 2 rows in set (0.00 sec)
- Related Articles
- Check if the column values are the same among multiple records and set these records in a single row separated by a special character in MySQL
- Set different IDs for records with conditions using a single MySQL query
- Finding a specific row which has several ids separated by comma in MySQL?
- Concatenate multiple rows and columns in a single row with MySQL
- Get the first 10 rows followed by the syntax to display remaining row records with a single MySQL query
- Find the records with % character in a LIKE query with MySQL
- Using GROUP BY and COUNT in a single MySQL query to group duplicate records and display corresponding max value
- Delete records from a MySQL table with IN() in a single query
- MySQL update multiple records in a single query?
- Insert all the values in a table with a single MySQL query separating records by comma
- Display all the column values in a single row separated by comma in MySQL?
- Get values from all rows and display it a single row separated by comma with MySQL
- MySQL query to display a substring before a special character in a string
- MySQL query to return all items in a single row
- MySQL query to select a row which contains same number in a column with set of numbers separated by comma?

Advertisements