连接Mysql中的字段
我有一张这样的桌子连接Mysql中的字段,mysql,sql,select,string-concatenation,Mysql,Sql,Select,String Concatenation,我有一张这样的桌子 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 name value 1 Ram a 2 John b 3 Ram c 4 Ram d 5 John e nam
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 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\u concat
select
name, group_concat(value separator ',') as value
from table_name
group by name
此外,如果您希望对值进行排序,您可以在组concat
中使用order by
select
name, group_concat(value order by value) as value
from table_name
group by name
使用以下命令:
SELECT field1, field2, GROUP_CONCAT(value ORDER BY value SEPARATOR ',')
AS value FROM table
GROUP BY field1, field2;
使用
该函数应为您提供以下技巧:
SELECT name, GROUP_CONCAT(value) AS value
FROM my_table
GROUP BY name
Mysql GROUP_CONCAT可以帮助您。请参阅此链接中的示例您必须使用
也不要更改原始问题第一个答案对于提供的问题非常正确。这并不能回答修改后的问题。@TimBiegeleisen检查我评论中的小提琴链接,该链接基本上回答了修改后的问题。:)按字段1、字段2分组