MySQL:排序GROUP_CONCAT值

时间:2009-06-15 10:20:51

标签: mysql sorting sql-order-by group-concat

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

查询:

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

我得到这一行:

  

工艺品»细木工

     

管理»组织

我想这样:

  

管理»组织

     

工艺品»细木工

2 个答案:

答案 0 :(得分:360)

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

答案 1 :(得分:20)

你的意思是订购吗?

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;