我有一张桌子,像:
+-----------+-------+------------+ | 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 | +-----------+-------------------+----------------------------+
您可以通过以下方式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
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。