MySQL: Sort GROUP_CONCAT values

别等时光非礼了梦想. 提交于 2019-11-26 07:00:28

问题


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


回答1:


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;



回答2:


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;


来源:https://stackoverflow.com/questions/995373/mysql-sort-group-concat-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!