mansi
mansi

Reputation: 877

how to group_concat two columns

I have one table with two columns parent_string and child string like this

id  parent_string             child_string
 1    0|4#festivals$Festiv     NULL
 2    0|4#festivals$Festiv     1|4@5#diwali$Deepavali
 3    0|4#festivals$Festiv     1|4@6#christmas$Christmas
 4    0|8#birthday$Birthday    1|8@9#for-mom$For Mom
 5    0|8#birthday$Birthday    1|8@10#for-dad$For Dad

If i want to select id in(1,2,3) then

I want output concat string like following:

0|4#festivals$Festiv~1|4@5#diwali$Deepavali~1|4@6#christmas$Christmas

how to do this?

Upvotes: 0

Views: 312

Answers (1)

Ravinder Reddy
Ravinder Reddy

Reputation: 24012

You can use concat_ws and group concat with custom separator on the fields ...

Example:

select
       concat_ws( '|', parent_string, group_concat( child_string separator '|' ) )
  from table_ame
 group by parent_string

Refer to Documentation:
CONCAT_WS(separator,str1,str2,...)

  • CONCAT_WS() stands for Concatenate With Separator and is a special form of CONCAT(). The first argument is the separator for the rest of the arguments. The separator is added between the strings to be concatenated. The separator can be a string, as can the rest of the arguments. If the separator is NULL, the result is NULL

GROUP_CONCAT(expr)

  • This function returns a string result with the concatenated non-NULL values from a group. It returns NULL if there are no non-NULL values

Upvotes: 1

Related Questions