我有一张这样的 table
id name value
1 Ram a
2 John b
3 Ram c
4 Ram d
5 John e
我想要这样的输出
name value
Ram a,c,d
John b,e
有什么方法可以执行这个查询吗?
更新:
表格格式:
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
输出:
field1 field2 value
val1 null a,c
val2 null b,d
null val1 e,f
null val2 g,h
有什么办法可以做到这一点吗?
最佳答案
你可以使用group_concat
select
name, group_concat(value separator ',') as value
from table_name
group by name
此外,如果您希望对值进行排序,您可以在 group concat
as 中使用 order by
as
select
name, group_concat(value order by value) as value
from table_name
group by name
关于mysql - 连接Mysql中的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30474663/