是的,您可以为此使用group_concat()。让我们首先创建一个表-mysql> create table groupByOneSelectAll
-> (
-> StudentDetails varchar(100),
-> StudentName varchar(100)
-> );
以下是使用insert命令在表中插入一些记录的查询-mysql> insert into groupByOneSelectAll values('StudentFirstName','John');
mysql> insert into groupByOneSelectAll values('StudentFirstName','Chris');
mysql> insert into groupByOneSelectAll values('StudentFirstName','Robert');
mysql> insert into groupByOneSelectAll values('StudentFirstName','Bob');
mysql> insert into groupByOneSelectAll values('StudentFirstName','David');
以下是使用select语句显示表中所有记录的查询-mysql> select * from groupByOneSelectAll;
这将产生以下输出-+------------------+-------------+
| StudentDetails | StudentName |
+------------------+-------------+
| StudentFirstName | John |
| StudentFirstName | Chris |
| StudentFirstName | Robert |
| StudentFirstName | Bob |
| StudentFirstName | David |
+------------------+-------------+
5 rows in set (0.00 sec)
以下是按单个列分组的查询-mysql> select StudentDetails,group_concat(StudentName) from groupByOneSelectAll group
by StudentDetails;
这将产生以下输出-+------------------+-----------------------------+
| StudentDetails | group_concat(StudentName) |
+------------------+-----------------------------+
| StudentFirstName | John,Chris,Robert,Bob,David |
+------------------+-----------------------------+
1 row in set (0.05 sec)