【问题标题】:mysql group_concat (sum)mysql group_concat(总和)
【发布时间】:2022-01-14 11:26:15
【问题描述】:

标签:

name    time                data

AAA    2021-10-1 13:05:00   11
AAA    2021-10-1 13:05:00   20
AAA    2021-10-1 14:10:00   35
BBB    2021-10-1 13:05:00   20
BBB    2021-10-1 13:05:00   20
BBB    2021-10-1 14:10:00   10
CCC    2021-10-1 14:10:00   8

我试过了:

select name,group_concat(data) from wip where time between '2021-10-26 00:00:00' 和 '2021-10-26 23:59:59' GROUP BY name

结果:

name   data
AAA   11,20,35
BBB   20,20,10
CCC   8

希望 sum(data) 按小时(时间)分组结果如下:

name   data
AAA    [31,35]
BBB    [40,10]
CCC    [ 0,18]

【问题讨论】:

  • 在输出中显示 0 有什么价值?也许使用 group_concat 显示什么是合适的。
  • group_concat(sum(data))
  • select name,group_concat(sum(data)) from wip where time between '2021-10-26 00:00:00' and '2021-10-26 23:59:59' GROUP BY名称
  • 嵌套选择--inner: SUM(data) GROUP BY time;使用 GROUP_CONCAT 的外层。

标签: mysql group-concat


【解决方案1】:

需要两个步骤:

SELECT name, GROUP_CONCAT(sum_data)
    FROM ( SELECT name, time, SUM(data) AS sum_data
              FROM tbl GROUP BY name, time ) AS x

(而且我不认为“旋转”是相关的。)

如果需要括号,请使用CONCAT("[", GROUP_CONCAT(sum_data), "]")

【讨论】:

    猜你喜欢
    • 2013-03-08
    • 1970-01-01
    • 2014-05-25
    • 1970-01-01
    • 2012-07-28
    • 2014-01-14
    • 2011-01-31
    • 2013-03-28
    • 2011-02-03
    相关资源
    最近更新 更多