在MySQL中将多个文本记录合并为一个
要合并多个文本记录,请使用GROUP_CONCAT()。让我们首先创建一个表-
create table DemoTable1611 -> ( -> Value text -> );
使用插入命令在表中插入一些记录-
insert into DemoTable1611 values('John'); insert into DemoTable1611 values('is'); insert into DemoTable1611 values('learning'); insert into DemoTable1611 values('Java'); insert into DemoTable1611 values('with'); insert into DemoTable1611 values('MySQL Database');
使用select语句显示表中的所有记录-
select * from DemoTable1611;
这将产生以下输出-
+----------------+ | Value | +----------------+ | John | | is | | learning | | Java | | with | | MySQL Database | +----------------+ 6 rows in set (0.00 sec)
以下是合并多个文本记录的查询-
select group_concat(Value separator ' ') from DemoTable1611;
这将产生以下输出
+-------------------------------------------+ | group_concat(Value separator ' ') | +-------------------------------------------+ | John is learning Java with MySQL Database | +-------------------------------------------+ 1 row in set (0.00 sec)