Home >Database >Mysql Tutorial >How Can I Sort the Results of a GROUP_CONCAT() Function in MySQL?
Can GROUP_CONCAT() Results Be Sorted in MySQL?
Sorting the values returned by a GROUP_CONCAT() statement is indeed possible in MySQL. By leveraging the ORDER BY clause within the subquery portion of the GROUP_CONCAT() expression, you can dictate the sort order of the concatenated values.
Implementation:
SELECT student_name, GROUP_CONCAT(DISTINCT test_score ORDER BY test_score DESC SEPARATOR ' ') FROM student GROUP BY student_name;
In this example:
This approach allows you to control the sorting criteria within the GROUP_CONCAT() statement, ensuring the desired order of the concatenated values.
The above is the detailed content of How Can I Sort the Results of a GROUP_CONCAT() Function in MySQL?. For more information, please follow other related articles on the PHP Chinese website!