【发布时间】:2012-12-02 15:46:24
【问题描述】:
如何将多个值存储在 mysql 用户定义的变量中
一般来说,
select @a:=color from tabex;
其中tabex如下
mysql> select * from tabex;
+----+----------+-------+-----------+
| id | personid | color | color_set |
+----+----------+-------+-----------+
| 1 | 1 | red | red,white |
| 2 | 1 | white | red,white |
| 3 | 2 | blue | NULL |
| 4 | 2 | red | NULL |
+----+----------+-------+-----------+
然后如果我执行查询之后
mysql> select @a;
+------+
| @a |
+------+
| red |
+------+
我得到了上面的结果,但实际上我想要下面的结果
+-------+
| @a |
+-------+
| red |
| white |
| blue |
| red |
+-------+
请任何人告诉我,这在 mysql 中是否可行。
只是我的问题是如何将多个值存储在 mysql 用户定义的变量中
【问题讨论】:
-
这是可能的。用户定义的变量存储一个值。所以要存储多个值,您必须创建一个连接字符串。
GROUP_CONCAT(color)可以解决问题。