要在group_concat()函数中使用换行符,请遵循以下语法-
select group_concat(concat_ws(' ',yourColumnName1, yourColumnName2) SEPARATOR "\r\n") from yourTableName;
让我们首先创建一个表-
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, FirstName varchar(20), LastName varchar(20) );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable(FirstName,LastName) values('John','Smith'); mysql> insert into DemoTable(FirstName,LastName) values('David','Miller'); mysql> insert into DemoTable(FirstName,LastName) values('John','Doe');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+----+-----------+----------+ | Id | FirstName | LastName | +----+-----------+----------+ | 1 | John | Smith | | 2 | David | Miller | | 3 | John | Doe | +----+-----------+----------+ 3 rows in set (0.00 sec)
以下是与group_concat函数的新行分隔符一起使用的查询-
mysql> select group_concat(concat_ws(' ',FirstName, LastName) SEPARATOR "\r\n") from DemoTable;
这将产生以下输出&mnus ;;
+-------------------------------------------------------------------+ | group_concat(concat_ws(' ',FirstName, LastName) SEPARATOR "\r\n") | +-------------------------------------------------------------------+ | John Smith David Miller John Doe | +-------------------------------------------------------------------+ 1 row in set (0.00 sec)