GROUP_CONCAT comma separator - MySQL

MysqlGroup ConcatSeparator

Mysql Problem Overview


I have a query where I am using GROUP_CONCAT and a custom separator as my results may contain commas: '----'

This all works well, however it is still comma separated, so my output is:

Result A----,Result B----,Result C----

How can I make it so the output is:

Result A----Result B----Result C----

I thought this was the idea of a custom separator!

Failing that, can you escape commas in your results, so I can explode in PHP by the GROUP_CONCAT commas?

Mysql Solutions


Solution 1 - Mysql

Looks like you're missing the SEPARATOR keyword in the GROUP_CONCAT function.

GROUP_CONCAT(artists.artistname SEPARATOR '----')

The way you've written it, you're concatenating artists.artistname with the '----' string using the default comma separator.

Solution 2 - Mysql

Query to achieve your requirment

SELECT id,GROUP_CONCAT(text SEPARATOR ' ') AS text FROM table_name group by id;

Solution 3 - Mysql

Or, if you are doing a split - join:

GROUP_CONCAT(split(thing, " "), '----') AS thing_name,

You may want to inclue WITHIN RECORD, like this:

GROUP_CONCAT(split(thing, " "), '----') WITHIN RECORD AS thing_name,

from BigQuery API page

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser984580View Question on Stackoverflow
Solution 1 - MysqlJoe StefanelliView Answer on Stackoverflow
Solution 2 - MysqlVallabh BothreView Answer on Stackoverflow
Solution 3 - MysqlRomanView Answer on Stackoverflow