Home > Enterprise >  "Group" Datatype, group_concat without a "group"
"Group" Datatype, group_concat without a "group"

Time:11-20

For group_concat the MySQL manual says:

This function returns a string result with the concatenated non-NULL values from a group.

group in this context seems like it is a datatype of some sort. Is it possible to create this data type manually?

An example, I have one and two in a table and I want to retrieve those two values separated as a CSV:

select group_concat(column) from table group by column

This concatenates

'one', 'two'

as expected, however:

select group_concat('one', 'two');

returns:

'onetwo'

as if it were just concat.

CodePudding user response:

The group is not a data type. In this context, it refers to a set of rows, defined by the rows with the same value in the column you name in the GROUP BY clause.

GROUP_CONCAT() with multiple arguments acts like CONCAT(), as mentioned in the comments above. That is, multiple arguments are concatenated into a single string.

You seem to want to concatenate multiple arguments with a comma separator, without taking the values from a set of rows. For this, you could use MySQL's string function CONCAT_WS():

select concat_ws(',', 'one', 'two') as my_list;

 --------- 
| my_list |
 --------- 
| one,two |
 --------- 
  • Related