Combine multiple text records to one in MySQL

MySQLMySQLi Database

To combine multiple text records, use GROUP_CONCAT(). Let us first create a table −

mysql> create table DemoTable1611
   -> (
   -> Value text
   -> );
Query OK, 0 rows affected (0.86 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1611 values('John');
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable1611 values('is');
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable1611 values('learning');
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable1611 values('Java');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable1611 values('with');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable1611 values('MySQL Database');
Query OK, 1 row affected (0.12 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1611;

This will produce the following output −

+----------------+
| Value          |
+----------------+
| John           |
| is             |
| learning       |
| Java           |
| with           |
| MySQL Database |
+----------------+
6 rows in set (0.00 sec)

Following is the query to combine multiple text records −

mysql> select  group_concat(Value separator ' ') from DemoTable1611;

This will produce the following output

+-------------------------------------------+
| group_concat(Value separator ' ')         |
+-------------------------------------------+
| John is learning Java with MySQL Database |
+-------------------------------------------+
1 row in set (0.00 sec)
raja
Published on 17-Dec-2019 06:04:48
Advertisements