MySQL: Sort GROUP_CONCAT values

MysqlSortingSql Order-ByGroup Concat

Mysql Problem Overview


In short: Is there any way to sort the values in a GROUP_CONCAT statement?

Query:

GROUP_CONCAT((SELECT GROUP_CONCAT(parent.name SEPARATOR " » ") 
FROM test_competence AS node, test_competence AS parent 
WHERE node.lft BETWEEN parent.lft AND parent.rgt 
  AND node.id = l.competence 
  AND parent.id != 1 
ORDER BY parent.lft) SEPARATOR "<br />\n") AS competences

I get this row:

> Crafts » Joinery > > Administration » Organization

I want it like this: > Administration » Organization > > Crafts » Joinery

Mysql Solutions


Solution 1 - Mysql

Sure, see http://dev.mysql.com/doc/refman/...tions.html#function_group-concat:

SELECT student_name,
  GROUP_CONCAT(DISTINCT test_score ORDER BY test_score DESC SEPARATOR ' ')
  FROM student
  GROUP BY student_name;

Solution 2 - Mysql

Do you mean to order by?

SELECT _key,            
COUNT(*) as cnt,            
GROUP_CONCAT(_value ORDER BY _value SEPARATOR ', ') as value_list      
FROM group_concat_test      
GROUP BY _key      
ORDER BY _key;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionIvarView Question on Stackoverflow
Solution 1 - MysqlSampsonView Answer on Stackoverflow
Solution 2 - MysqlHaim EvgiView Answer on Stackoverflow