【问题标题】:GROUP_CONCAT ORDER BYGROUP_CONCAT ORDER BY
【发布时间】:2012-01-27 16:59:39
【问题描述】:

我已经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 |
+-----------+-------------------+----------------------------+

【问题讨论】:

  • 表格中的视图值和百分比值是否以逗号分隔值存储?
  • 不,它们不是那样存储的,但我是通过查询生成的,即使用 group_concat

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


【解决方案1】:

你可以这样在GROUP_CONCAT函数内部使用ORDER BY

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

【讨论】:

  • 不应该是group_concat(li.percentage ORDER BY li.views ASC),以便按照原始帖子的要求按照与视图匹配的顺序显示百分比吗?
  • @aleroot,这个查询是 MySQL 特定的吗?
  • 是的,GROUP_CONCAT 是 MySQL 独有的函数,但是在其他数据库上可以通过代理函数实现类似的结果...
  • 如果使用与 group_concat() 相同的表达式对 group_concat 进行排序,则只需编写 group_concat(li.views ORDER BY 1)。最简单!
  • 如果您还使用分隔符:SELECT GROUP_CONCAT(CONCAT(u.RegionName,'-',u.UserName) ORDER BY u.RegionName SEPARATOR '@@')
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    试试

    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

    【讨论】:

      【解决方案4】:

      您可以通过这种方式在 GROUP_CONCAT 函数中使用 SEPARATORORDER 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;
      

      【讨论】:

        【解决方案5】:

        在 IMPALA 中,在 GROUP_CONCAT 中没有订单可能会有问题,在 Coders'Co.我们有某种解决方法(我们需要它用于 Rax/Impala)。如果您需要 IMPALA 中带有 ORDER BY 子句的 GROUP_CONCAT 结果,请查看这篇博文:http://raxdb.com/blog/sorting-by-regex/

        【讨论】:

        猜你喜欢
        • 2015-06-28
        • 2013-02-04
        • 1970-01-01
        • 2011-10-29
        • 2015-09-11
        • 1970-01-01
        • 2014-01-30
        • 1970-01-01
        • 2012-04-07
        相关资源
        最近更新 更多