在位字段上使用GROUP_CONCAT()在MySQL中返回垃圾?怎么修?

要解决此问题,请在group_concat()的列中加上0。让我们首先创建一个表-

mysql> create table DemoTable1856
     (
     Id int,
     Value bit(1)
     );

使用插入命令在表中插入一些记录-

mysql> insert into DemoTable1856 values(101,1);
mysql> insert into DemoTable1856 values(102,0);
mysql> insert into DemoTable1856 values(101,0);
mysql> insert into DemoTable1856 values(102,1);
mysql> insert into DemoTable1856 values(101,0);

使用select语句显示表中的所有记录-

mysql> select * from DemoTable1856;

这将产生以下输出-

+------+-------+
| Id   | Value |
+------+-------+
|  101 |       |
|  102 |       |
|  101 |       |
|  102 |       |
|  101 |       |
+------+-------+
5 rows in set (0.00 sec)

这是在位字段上使用group_concat()并避免返回垃圾值的查询-

mysql> select group_concat(Value+0) from DemoTable1856
     group by Id;

这将产生以下输出-

+-----------------------+
| group_concat(Value+0) |
+-----------------------+
| 1,0,0                 |
| 0,1                   |
+-----------------------+
2 rows in set (0.00 sec)