The function in SQL that converts multiple rows of values into one row is ARRAY_TO_STRING(), which converts an array into a comma-separated string. Used in conjunction with aggregate functions such as GROUP_CONCAT(), data from multiple rows can be grouped and converted into a single row.
Function in SQL to convert multi-row values into one row
Question: How Convert multi-row values to one row in SQL?
Answer: You can use the SQL function ARRAY_TO_STRING() to convert multiple rows of values into a single row of strings.
Detailed answer:
ARRAY_TO_STRING() function converts an array into a comma-separated string. This function can be used in conjunction with other aggregate functions such as GROUP_CONCAT() to group and convert data from multiple rows into a single row.
Syntax:
<code>ARRAY_TO_STRING(array_column, separator)</code>
Where:
Example:
Suppose we have a table called "students" with the following columns:
id | name |
---|---|
1 | Alice |
2 | Bob |
3 | Carol |
You can use the following query to list the student names Convert to a comma separated string:
<code>SELECT ARRAY_TO_STRING(name, ', ') AS student_names FROM students;</code>
Output:
student_names |
---|
Alice, Bob, Carol |
Similarly, the following query separates student names with semicolons:
<code>SELECT ARRAY_TO_STRING(name, '; ') AS student_names FROM students;</code>
Output:
student_names |
---|
Alice; Bob; Carol |
The above is the detailed content of Function that converts multi-row values into one row in sql. For more information, please follow other related articles on the PHP Chinese website!