SQL Combine Multiple Rows into One
In SQL, you can use the GROUP BY clause and aggregate functions to combine multiple rows into one row.
For example, considering the following data:
id name
1 John
1 Jane
1 Mike
2 Anna
2 David
To merge the above data into one row by id, you can use the GROUP BY clause and GROUP_CONCAT function.
SELECT id, GROUP_CONCAT(name) AS names
FROM table_name
GROUP BY id;
The above query will yield the following results:
id names
1 John,Jane,Mike
2 Anna,David
The GROUP_CONCAT function combines the name values under each id into a single string, separating each value with a comma.
It should be noted that the GROUP_CONCAT function may have different syntax or details in different databases, so adjust accordingly based on the type and version of the database used.