简体   繁体   中英

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. If I removed one concat (at random) it works.

The table contains about 15 million rows. I have set

SET SESSION group_concat_max_len = 188446744073709551615;

Any ideas why it return 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.

UPDATE : SEPERATOR was a typo - it should be SEPARATOR .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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