Concatenate fields in Mysql

I have a table like this

id   name   value
1    Ram     a
2    John    b
3    Ram     c
4    Ram     d
5    John    e

I want this conclusion to look like

name   value
Ram     a,c,d
John    b,e

Is there any way to fulfill this request?

UPDATE:

Table format:

id   field1   value  field2
1    val1     a       null
2    val2     b       null
3    val1     c       null
4    val2     d       null
5    null     e       val1
5    null     f       val1
5    null     g       val2
5    null     h       val2

Output:

field1   field2   value
val1      null    a,c
val2      null    b,d
null      val1    e,f
null      val2    g,h

Is there any way to accomplish this?

+4
source share
6 answers

you can use group_concat

select
name, group_concat(value separator ',') as value
from table_name
group by name

Also, if you want the values ​​to be ordered, you can use order byinternally group concatas

select
name, group_concat(value order by value) as value
from table_name
group by name
+5
source

Use this:

SELECT field1, field2, GROUP_CONCAT(value ORDER BY value SEPARATOR ',')
AS value FROM table
GROUP BY field1, field2;
+1
source

group_concat()

SELECT name, GROUP_CONCAT(value) AS value FROM my_table group by name
+1

group_concat :

SELECT   name, GROUP_CONCAT(value) AS value
FROM     my_table
GROUP BY name
+1

group_concat

select name,group_concat(value) from tablename group by name
+1

All Articles