# mysql数据类型-集合

SET是一个字符串对象，可以包含0个或多个值，每一个值都必须是在建表是指定的取值列表中出现的。SET类型的列的值可以是建表是取值列表中的一个或多个值，每个值用’,’分割。由此可以得出一个结论，集合中的元素不能包含’,’。

MySQL以数字方式存储SET值，字节序的低位（1）对应于第一个成员，以此类推。 如果在数字上下文中查询SET类型列的值，则查询到的列值为所有bit位组成的数字。 例如，你可以以下面所示的方式查询SET类型的列：

SET Member Decimal Value Binary Value
‘a’ 1 0001
‘b’ 2 0010
‘c’ 4 0100
‘d’ 8 1000

SET values are sorted numerically. NULL values sort before non-NULL SET values.

Functions such as SUM() or AVG() that expect a numeric argument cast the argument to a number if necessary. For SET values, the cast operation causes the numeric value to be used.

Normally, you search for SET values using the FIND_IN_SET() function or the LIKE operator:

The first statement finds rows where set_col contains the value set member. 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.

The following statements also are permitted:

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 ‘val1,val2’ returns different results than comparing values to ‘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 tbl_name LIKE set_col and parse the SET definition in the Type column of the output.

In the C API, SET values are returned as strings. For information about using result set metadata to distinguish them from other strings, see Section 27.8.5, “C API Data Structures”.