在 MySQL GROUP_CONCAT 中使用函数进行排序
我想在 GROUP_CONCAT 函数中对结果进行排序。问题是,GROUP_CONCAT 函数中的选择是另一个函数,就像这样(幻想选择):
SELECT a.name,
GROUP_CONCAT(DISTINCT CONCAT_WS(':', b.id, c.name) ORDER BY b.id ASC) AS course
FROM people a, stuff b, courses c
GROUP BY a.id
我想要得到像(按 b.id 排序)这样的结果:
michael 1:science,2:maths,3:physics
但我得到:
michael 2:maths,1:science,3:physics
有谁知道我如何排序b.id 在我的 group_concat 中吗?
I want to order the results in a GROUP_CONCAT function. The problem is, that the selection in the GROUP_CONCAT-function is another function, like this (fantasy select):
SELECT a.name,
GROUP_CONCAT(DISTINCT CONCAT_WS(':', b.id, c.name) ORDER BY b.id ASC) AS course
FROM people a, stuff b, courses c
GROUP BY a.id
I want to get a result like (ordered by b.id):
michael 1:science,2:maths,3:physics
but I get:
michael 2:maths,1:science,3:physics
Does anyone know how I can order by b.id in my group_concat here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果有人关心的话,我想我至少找到了类似问题的解决方案。
http://mahmudahsan.wordpress.com/2008/08/ 27/mysql-the-group_concat-function/
如果有分隔符,则 order by 进入 group_concat 之前的分隔符。
If anyone cares, I think I found a solution for at least a similar problem.
http://mahmudahsan.wordpress.com/2008/08/27/mysql-the-group_concat-function/
The order by goes in the group_concat BEFORE the separator if there is one.
我知道这确实很老了,但现在我正在寻找答案,@korny 的答案给了我这个想法:(
如果不清楚的话,它对我有用:-))
I know this is really old, but just now I was looking for an answer and @korny's answer gave me the idea for this:
(And it works for me, if that wasn't clear :-) )
我不知道执行此操作的标准方法。这个查询有效,但恐怕它只取决于一些实现细节:
I don't know of a standard way to do this. This query works, but I'm afraid it just depends on some implementation detail:
不需要子选择。
No need for subselects.