Skip to content
Advertisement

how may I concatenate the columns in this script in MySQL?

Below is a SQL query that i am using to concatenate columns in SQL Server. How may I concatenate the same in MySQL?

 SELECT Name, STRING_AGG(school,',') AS School
,STRING_AGG(class,',') AS Class
,STRING_AGG(description,',') AS Description
,MIN(start_date) AS Start_date
,MAX(end_date) AS End_date FROM source_table GROUP BY Name

Advertisement

Answer

The equivalent function in MySQL is group_concat():

SELECT Name, GROUP_CONCAT(school) AS Schools,
       GROUP_CONCAT(class) AS Classes,
       GROUP_CONCAT(description) AS Descriptions,
       MIN(start_date) AS Start_date,
       MAX(end_date) AS End_date
FROM source_table
GROUP BY Name;

The default separate is a comma, so there is no need to explicitly provide it.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement