【问题标题】:MySQL: Replace null with 0 in GROUP_CONCATMySQL:将 GROUP_CONCAT 中的 null 替换为 0
【发布时间】:2018-12-28 07:44:46
【问题描述】:

我有一张桌子叫trx

trx_year  trx_month  Product  number_of_trx 
2018      4          A        100
2018      5          A        300
2018      3          A        500
2018      1          A        200
2018      2          A        150
2018      5          B        400
2018      2          B        200
2018      1          B        350

我想要结果:

按月升序排列的 trx 数量的产品

我有一个这样的查询:

select product,GROUP_CONCAT(number_of_trx order by trx_month)
from trx
where trx_year=2018
group by product

该查询的结果:

Product  Data
A     200,150,500,100,300
B     350,200,400

但是,我想要这样的结果:(将月份的空值替换为 0)

Product  Data
A     200,150,500,100,300
B     350,200,0,0,400

我已经尝试过ifnull()coalesce()这样的:(但结果和以前一样)

select product,GROUP_CONCAT(ifnull(number_of_trx,0) order by trx_month)
from trx
where trx_year=2018
group by product;

select product,GROUP_CONCAT(coalesce(number_of_trx,0) order by trx_month)
from trx
where trx_year=2018
group by product;

也许你可以帮助我,请查看http://sqlfiddle.com/#!9/f1ed4/3

【问题讨论】:

  • 为什么中间有 0,0 ?有什么规定吗?
  • 您的数据集中没有空值
  • @ZaynulAbadinTuhin 因为最大月份是 5(五月),并且产品 B 在第 3 个月和第 4 个月没有 trx
  • @Strawberry 产品B有空值,第3、4个月没有交易
  • 这不是一回事......

标签: mysql sql database group-concat sqlfiddle


【解决方案1】:

这就是我想出的。可能会更有效率,但你可以从中获得灵感。加入产品表,而不是选择不同的产品。还扩展到包括超过 5 个月的月份。

SELECT trx2.product, GROUP_CONCAT(trx2.total order by trx2.trx_month)
FROM
(SELECT temp2.product, temp2.trx_month, SUM(temp2.number_of_trx) AS total
FROM
(SELECT products.product, temp1.trx_month, temp1.number_of_trx
FROM (select 1 as trx_month, 0 as number_of_trx
UNION select 2, 0
UNION select 3, 0
UNION select 4, 0
UNION select 5, 0) as temp1,
(SELECT distinct product from trx) AS products

UNION ALL

SELECT trx.product, trx.trx_month, trx.number_of_trx
FROM trx) as temp2
GROUP BY temp2.product, temp2.trx_month) AS trx2
GROUP BY product

【讨论】:

【解决方案2】:

Group by 将表格内容与可用值分组。 对于产品 A:月 1 2 3 4 5 可用并且 对于产品 B:月 1 2 5 可用

它不会针对产品 B 自动填写第 3 个月和第 4 个月的表格

解决这个问题 要么你必须用 B - 3 和 B - 4 和 0 值来填充这个表,作为 trx 的数量 或者您可以创建临时表并在其中执行相同的操作。

为了实现这一点, 1.您将不得不使用日期功能(将您的月份和年份值转换为日期) 2. 为每个循环增加月份值,如果月份值不能与当前产品值相比,则在表中插入记录。 3. 然后在这个更新的表上通过查询运行你的组,它会给你所需的结果。

这是一个有点复杂的任务来实现。您将不得不进行多次检查。

如果我能找到任何其他解决方案,我会发布。谢谢。

【讨论】:

  • 另一种方法:您可以创建另一个包含产品和月份值的表,因此每个产品有 12 行并将该表与 trx 连接。
  • 你们觉得我的回复怎么样?
  • 你能在我的fiddle 中试试你的想法吗? @HardikNuwal
【解决方案3】:

使用cross join 生成您想要的所有行。那将是所有产品/月份的组合。然后用left join引入数据,用group by压缩:

select p.product,
       group_concat(coalesce(trx.number_of_trx, 0) order by trx_month)
from (select distinct product from trx) p cross join
     (select distinct trx_year, trx_month
      from trx
      where trx_year = 2018
     ) yyyymm left join
     trx
     on trx.product = p.product and
        trx.trx_year = yyyymm.trx_year
        trx.trx_month = yyyymm.trx_month
group by p.product

注意group_concat() 中的order by。如果您希望按时间顺序排列结果,这一点非常重要。

【讨论】:

    【解决方案4】:

    选择产品,GROUP_CONCAT(number_of_trx) order by trx_month), group_concat(当 trx_year = 2018 then number_of_trx else 0 end)作为数据 来自 trx 按产品分组;

    【讨论】:

      【解决方案5】:

      如果 concat 为 null 则返回 0 否则返回值

      设置会话 group_concat_max_len = 10000000;
      SELECT coalesce(group_concat(DISTINCT columnname),0) 作为值

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-02
        • 1970-01-01
        • 1970-01-01
        • 2013-05-26
        相关资源
        最近更新 更多