有没有办法组合两个属于“set”类型的mysql字段?

时间:2014-02-12 19:55:30

标签: mysql

有没有办法合并两个字段......两种集合类型?

field1 is ('c')
field2 is ('a')

我想选择该字段,以便它返回'a,c'

我已经尝试了concat,但它会在集合中返回重复项。 因此,如果字段2是('a','c'),它将返回'a,c,c'

我也尝试了field1 & field2,但因为这是一个集合,所以会返回小数值。

1 个答案:

答案 0 :(得分:1)

尝试使用field1 | field2代替field1 & field2

mysql> create table test (id serial, foo set('a','b','c') not null, bar set('a','b','c') not null);
Query OK, 0 rows affected (0.10 sec)

mysql> insert into test (foo,bar) values('a,c','c,b'),('b,a','a'),('a,b','b,c');
Query OK, 3 rows affected (0.05 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from test;
+----+-----+-----+
| id | foo | bar |
+----+-----+-----+
|  1 | a,c | b,c |
|  2 | a,b | a   |
|  3 | a,b | b,c |
+----+-----+-----+
3 rows in set (0.01 sec)

mysql> update test set foo = foo | bar;
Query OK, 2 rows affected (0.16 sec)
Rows matched: 3  Changed: 2  Warnings: 0

mysql> select * from test;
+----+-------+-----+
| id | foo   | bar |
+----+-------+-----+
|  1 | a,b,c | b,c |
|  2 | a,b   | a   |
|  3 | a,b,c | b,c |
+----+-------+-----+
3 rows in set (0.00 sec)