示例:
建表:
USE test;
CREATE TABLE `test_table` (
`id` bigint NOT NULL AUTO_INCREMENT,
`data` varchar(32) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
插入数据:
insert into test_table (data) values('111');
insert into test_table (data) values('222');
查询示例:
mysql root@127.0.0.1:test> select * from test_table
+----+------+
| id | data |
+----+------+
| 1 | 111 |
| 2 | 222 |
+----+------+
mysql root@127.0.0.1:test> select group_concat(data) as result from test_table;
+---------+
| result |
+---------+
| 111,222 |
+---------+
mysql root@127.0.0.1:test> select group_concat(data) as result from test_table order by id desc;
+---------+
| result |
+---------+
| 111,222 |
+---------+
mysql root@127.0.0.1:test> select group_concat(data order by id desc) as result from test_table;
+---------+
| result |
+---------+
| 222,111 |
+---------+
mysql root@127.0.0.1:test> select group_concat(data separator ';') as result from test_table;
+---------+
| result |
+---------+
| 111;222 |
+---------+