简体   繁体   English

如果添加了另一列,则group_concat返回null

[英]group_concat returns null, if another column added

I have the following code 我有以下代码

SELECT
  GROUP_CONCAT(
    CONCAT('{"id"', id, ','),
    CONCAT('"COL1"', col1, ','),
    CONCAT('"col2"', col2, ','),
    CONCAT('"col3"', col3, ','),
    CONCAT('"col4"', col4, '}')
    SEPERATOR '\n')
  AS json from tableX

The whole group_concat returns null. 整个group_concat返回null。 If I removed one concat (at random) it works. 如果我(随机)删除了一个concat,它将起作用。

The table contains about 15 million rows. 该表包含约1500万行。 I have set 我已经设定

SET SESSION group_concat_max_len = 188446744073709551615;

Any ideas why it return null? 任何想法为什么它返回null?

Try this, handling any null values and seeing if your output is indeed exceeding the length threshold: 尝试此操作,处理所有空值,然后查看您的输出是否确实超过了长度阈值:

  select json, length(json) from (
  SELECT
  GROUP_CONCAT(
    CONCAT('{"id"', coalesce(id, '""'), ','),
    CONCAT('"COL1"', coalesce(col1, '""'), ','),
    CONCAT('"col2"', coalesce(col2, '""'), ','),
    CONCAT('"col3"', coalesce(col3, '""'), ','),
    CONCAT('"col4"', coalesce(col4, '""'), '}')
    SEPARATOR '\n')
  AS json from tableX
  ) x
  -- limit 200000

If the group_concat function lets NULL values override everything else (I'm not sure about that), then you have it catered for. 如果group_concat函数让NULL值覆盖其他所有内容(我不确定),那么您就可以使用它。

UPDATE : SEPERATOR was a typo - it should be SEPARATOR . 更新SEPERATOR是一个错字-应该是SEPARATOR

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM