MySQL:排序 GROUP_CONCAT 值

2021-11-20 00:00:00 sorting mysql sql-order-by group-concat

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

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

查询:

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

我得到这一行:

工艺品 »细木工

Crafts » Joinery

管理»组织

Administration » Organization

我想要这样:

管理»组织

Administration » Organization

工艺品 »细木工

Crafts » Joinery

推荐答案

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

相关文章