我a table喜欢:
+-----------+-------+------------+
| 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 |
+-----------+-------------------+----------------------------+
答案 0 :(得分:245)
您可以通过以下方式在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
答案 1 :(得分:14)
group_concat支持自己的order by子句
http://mahmudahsan.wordpress.com/2008/08/27/mysql-the-group_concat-function/
所以你应该写:
SELECT li.clientid, group_concat(li.views order by views) AS views,
group_concat(li.percentage order by percentage)
FROM table_views GROUP BY client_id
答案 2 :(得分:9)
尝试
SELECT li.clientid, group_concat(li.views ORDER BY li.views) AS views,
group_concat(li.percentage ORDER BY li.percentage)
FROM table_views li
GROUP BY client_id
http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function%5Fgroup-concat
答案 3 :(得分:1)
在IMPALA中,没有在GROUP_CONCAT中订购可能会在Coders'Co上出现问题。我们有一些解决方法(我们需要它用于Rax / Impala)。如果您需要在IMPALA中使用ORDER BY子句的GROUP_CONCAT结果,请查看此博客文章:http://raxdb.com/blog/sorting-by-regex/
答案 4 :(得分:0)
您可以在 GROUP_CONCAT 函数内使用 SEPARATOR 和 ORDER BY :
SELECT li.client_id, group_concat(li.percentage ORDER BY li.views ASC SEPARATOR ',')
AS views, group_concat(li.percentage ORDER BY li.percentage ASC SEPARATOR ',') FROM li
GROUP BY client_id;