开发者社区> 问答> 正文

GROUP_CONCAT ORDER BY?mysql

我有一张桌子,像:

+-----------+-------+------------+ | client_id | views | percentage | +-----------+-------+------------+ | 1 | 6 | 20 | | 1 | 4 | 55 | | 1 | 9 | 56 | | 1 | 2 | 67 | | 1 | 7 | 80 | | 1 | 5 | 66 | | 1 | 3 | 33 | | 1 | 8 | 34 | | 1 | 1 | 52 | 我试过了group_concat:

SELECT li.client_id, group_concat(li.views) AS views,
group_concat(li.percentage) FROM li GROUP BY client_id;

+-----------+-------------------+-----------------------------+ | client_id | views | group_concat(li.percentage) | +-----------+-------------------+-----------------------------+ | 1 | 6,4,9,2,7,5,3,8,1 | 20,55,56,67,80,66,33,34,52 | +-----------+-------------------+-----------------------------+ 但我想按顺序获取视图,例如:

+-----------+-------------------+----------------------------+ | client_id | views | percentage | +-----------+-------------------+----------------------------+ | 1 | 1,2,3,4,5,6,7,8,9 | 52,67,33,55,66,20,80,34,56 | +-----------+-------------------+----------------------------+

展开
收起
保持可爱mmm 2020-05-17 11:52:01 553 0
1 条回答
写回答
取消 提交回答
  • 您可以通过以下方式ORDER BY在GROUP_CONCAT函数内部使用:

    SELECT li.client_id, group_concat(li.percentage ORDER BY li.views ASC) AS views, group_concat(li.percentage ORDER BY li.percentage ASC) FROM li GROUP BY client_id来源:stack overflow

    2020-05-17 12:01:14
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
One Box: 解读事务与分析一体化数据库 HybridDB for MySQL 立即下载
One Box:解读事务与分析一体化数据库HybridDB for MySQL 立即下载
如何支撑HTAP场景-HybridDB for MySQL系统架构和技术演进 立即下载

相关镜像