How to implement mysql multi-line to multiple columns?

In MySQL, you can use the GROUP_CONCAT function to merge multiple rows of data into one row and split it into multiple columns.

For example, consider the following data table:

+----+-------+---------+
| ID | Name  | Country |
+----+-------+---------+
| 1  | John  | USA     |
| 2  | Alice | UK      |
| 3  | Tom   | Japan   |
+----+-------+---------+

You can merge the Name column into one column using the GROUP_CONCAT function, and merge the Country column into another column.

SELECT GROUP_CONCAT(Name) AS Names, GROUP_CONCAT(Country) AS Countries
FROM table_name;

The output result is:

+-----------------+---------------------+
| Names           | Countries           |
+-----------------+---------------------+
| John,Alice,Tom  | USA,UK,Japan        |
+-----------------+---------------------+

Please note that GROUP_CONCAT by default uses a comma as a separator, but you can specify a different separator using the SEPARATOR parameter.

bannerAds