A SET
is a string object that can have zero or more values, each of which must be chosen from a list of permitted values specified when the table is created. SET
是可以有零个或多个值的字符串对象,每个值必须从创建表时指定的允许值列表中选择。由多个集合成员组成的SET
column values that consist of multiple set members are specified with members separated by commas (,
). SET
列值由逗号(,
)分隔的成员指定。A consequence of this is that 这样做的结果是SET
member values should not themselves contain commas.SET
成员值本身不应包含逗号。
For example, a column specified as 例如,一个指定为SET('one', 'two') NOT NULL
can have any of these values:SET('one', 'two') NOT NULL
的列可以具有这些值中的任一种:
'' 'one' 'two' 'one,two'
A 一个SET
column can have a maximum of 64 distinct members.SET
列最多可以有64个不同的成员。
Duplicate values in the definition cause a warning, or an error if strict SQL mode is enabled.如果启用了严格SQL模式,定义中的重复值将导致警告或错误。
Trailing spaces are automatically deleted from 创建表时,将自动从表定义中的SET
member values in the table definition when a table is created.SET
成员值中删除尾随空格。
See String Type Storage Requirements for storage requirements for the 有关SET
type.SET
类型的存储要求,请参阅字符串类型存储要求。
See Section 11.3.1, “String Data Type Syntax” for 有关SET
type syntax and length limits.SET
类型语法和长度限制,请参阅第11.3.1节,“字符串数据类型语法”。
When retrieved, values stored in a 检索时,存储在SET
column are displayed using the lettercase that was used in the column definition. SET
列中的值将使用列定义中使用的大小写显示。Note that 请注意,可以为SET
columns can be assigned a character set and collation. SET
列指定字符集和排序规则。For binary or case-sensitive collations, lettercase is taken into account when assigning values to the column.对于二进制或区分大小写的排序规则,在为列赋值时会考虑大小写。
MySQL stores MySQL以数字形式存储SET
values numerically, with the low-order bit of the stored value corresponding to the first set member. SET
值,存储值的低位对应于第一个集合成员。If you retrieve a 如果在数字上下文中检索SET
value in a numeric context, the value retrieved has bits set corresponding to the set members that make up the column value. SET
值,则检索到的值具有与构成列值的集成员相对应的位集。For example, you can retrieve numeric values from a 例如,可以从如下所示的SET
column like this:SET
列中检索数值:
mysql> SELECT set_col
+0 FROM tbl_name
;
If a number is stored into a 如果一个数字被存储到一个SET
column, the bits that are set in the binary representation of the number determine the set members in the column value. SET
列中,则该数字的二进制表示形式中设置的位决定了列值中的集合成员。For a column specified as 对于指定为SET('a','b','c','d')
, the members have the following decimal and binary values.SET('a','b','c','d')
的列,成员具有以下十进制和二进制值。
SET | ||
---|---|---|
'a' | 1 | 0001 |
'b' | 2 | 0010 |
'c' | 4 | 0100 |
'd' | 8 | 1000 |
If you assign a value of 如果将值9
to this column, that is 1001
in binary, so the first and fourth SET
value members 'a'
and 'd'
are selected and the resulting value is 'a,d'
.9
赋给该列,则二进制形式为1001
,因此选择第一个和第四个集值成员'a'
和'd'
,得到的值为'a,d'
。
For a value containing more than one 对于包含多个SET
element, it does not matter what order the elements are listed in when you insert the value. SET
元素的值,插入值时元素的排列顺序无关紧要。It also does not matter how many times a given element is listed in the value. 给定元素在值中列出多少次也无关紧要。When the value is retrieved later, each element in the value appears once, with elements listed according to the order in which they were specified at table creation time. 稍后检索该值时,该值中的每个元素都会出现一次,并根据在表创建时指定的顺序列出元素。Suppose that a column is specified as 假定列被指定为SET('a','b','c','d')
:SET('a','b','c','d')
:
mysql> CREATE TABLE myset (col SET('a', 'b', 'c', 'd'));
If you insert the values 如果插入值'a,d'
, 'd,a'
, 'a,d,d'
, 'a,d,a'
, and 'd,a,d'
:'a,d'
、'd,a'
、'a,d,d'
、'a,d,a'
和'd,a,d'
:
mysql> INSERT INTO myset (col) VALUES
-> ('a,d'), ('d,a'), ('a,d,a'), ('a,d,d'), ('d,a,d');
Query OK, 5 rows affected (0.01 sec)
Records: 5 Duplicates: 0 Warnings: 0
Then all these values appear as 检索时,所有这些值都显示为'a,d'
when retrieved:'a,d'
:
mysql> SELECT col FROM myset;
+------+
| col |
+------+
| a,d |
| a,d |
| a,d |
| a,d |
| a,d |
+------+
5 rows in set (0.04 sec)
If you set a 如果将SET
column to an unsupported value, the value is ignored and a warning is issued:SET
列设置为不受支持的值,则会忽略该值并发出警告:
mysql>INSERT INTO myset (col) VALUES ('a,d,d,s');
Query OK, 1 row affected, 1 warning (0.03 sec) mysql>SHOW WARNINGS;
+---------+------+------------------------------------------+ | Level | Code | Message | +---------+------+------------------------------------------+ | Warning | 1265 | Data truncated for column 'col' at row 1 | +---------+------+------------------------------------------+ 1 row in set (0.04 sec) mysql>SELECT col FROM myset;
+------+ | col | +------+ | a,d | | a,d | | a,d | | a,d | | a,d | | a,d | +------+ 6 rows in set (0.01 sec)
If strict SQL mode is enabled, attempts to insert invalid 如果启用了严格SQL模式,则尝试插入无效的SET
values result in an error.SET
值将导致错误。
SET
values are sorted numerically. SET
值按数字排序。NULL
values sort before non-NULL
SET
values.NULL
值排在非空SET
值之前。
Functions such as 如果需要,SUM()
or AVG()
that expect a numeric argument cast the argument to a number if necessary. SUM()
或AVG()
等需要数字参数的函数会将参数转换为数字。For 对于SET
values, the cast operation causes the numeric value to be used.SET
值,强制转换操作将使用数值。
Normally, you search for 通常,可以使用SET
values using the FIND_IN_SET()
function or the LIKE
operator:FIND_IN_SET()
函数或LIKE
运算符搜索集合值:
mysql>SELECT * FROM
mysql>tbl_name
WHERE FIND_IN_SET('value
',set_col
)>0;SELECT * FROM
tbl_name
WHEREset_col
LIKE '%value
%';
The first statement finds rows where 第一条语句查找set_col
contains the value
set member. set_col
包含value
集成员的行。The second is similar, but not the same: It finds rows where 第二种方法类似,但不相同:它可以在任意位置查找set_col
contains value
anywhere, even as a substring of another set member.set_col
包含值的行,甚至作为另一个集成员的子字符串。
The following statements also are permitted:也允许以下声明:
mysql>SELECT * FROM
mysql>tbl_name
WHEREset_col
& 1;SELECT * FROM
tbl_name
WHEREset_col
= 'val1
,val2
';
The first of these statements looks for values containing the first set member. 第一个语句查找包含第一个集合成员的值。The second looks for an exact match. 第二种是寻找完全匹配的。Be careful with comparisons of the second type. 小心第二种类型的比较。Comparing set values to 将集值与'
returns different results than comparing values to val1
,val2
''
. val2
,val1
''
进行比较会返回与将值与val1
,val2
''
进行比较不同的结果。val2
,val1
'You should specify the values in the same order they are listed in the column definition.您应该按照列定义中列出的顺序指定值。
To determine all possible values for a 要确定SET
column, use SHOW COLUMNS FROM
and parse the tbl_name
LIKE set_col
SET
definition in the Type
column of the output.SET
列的所有可能值,请使用SHOW COLUMNS FROM
并在输出的tbl_name
LIKE set_col
Type
列中解析SET
定义。
In the C API, 在C API中,SET
values are returned as strings. SET
值作为字符串返回。For information about using result set metadata to distinguish them from other strings, see C API Basic Data Structures.有关使用结果集元数据将其与其他字符串区分开来的信息,请参见C API基本数据结构。