MySQL:排序 GROUP_CONCAT 值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/995373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 13:30:05  来源:igfitidea点击:

MySQL: Sort GROUP_CONCAT values

mysqlsortingsql-order-bygroup-concat

提问by Ivar

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

简而言之:有没有办法对 GROUP_CONCAT 语句中的值进行排序?

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

管理 » 组织

工艺品 » 细木工

回答by Sampson

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

当然,请参阅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;

回答by Haim Evgi

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;