为此,请使用GROUP_CONCAT()和CONCAT()
。让我们首先创建一个表-
mysql> create table DemoTable1807 ( Id int );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable1807 values(101); mysql> insert into DemoTable1807 values(102); mysql> insert into DemoTable1807 values(103);
使用select语句显示表中的所有记录-
mysql> select * from DemoTable1807;
这将产生以下输出-
+------+ | Id | +------+ | 101 | | 102 | | 103 | +------+ 3 rows in set (0.00 sec)
这是将SQL查询转换为MySQL的查询-
mysql> select group_concat(concat('[',Id,']')) from DemoTable1807;
这将产生以下输出-
+----------------------------------+ | group_concat(concat('[',Id,']')) | +----------------------------------+ | [101],[102],[103] | +----------------------------------+ 1 row in set (0.00 sec)