MySQL MAKE_SET() 函数返回一个设置值(一个包含用","字符分隔的子字符串的字符串),该值由具有相应位设置的字符串组成。 str1、str2、... 中的 NULL 值不会附加到结果中。
语法
MAKE_SET(bits, str1, str2, str3,...)
参数
bits | 必填。 指定位集。 |
str1, str2, str3,... | 必填。 指定字符串列表。 |
返回值
返回一个设置值(包含由"分隔的子字符串的字符串" ," 字符),由设置了相应位的字符串组成。
示例 1:
下面的示例展示了 MAKE_SET() 函数的用法。
mysql> SELECT MAKE_SET(1, 'Learning', 'MySQL', 'is', 'fun');
Result: 'Learning'
mysql> SELECT MAKE_SET(1|2, 'Learning', 'MySQL', 'is', 'fun');
Result: 'Learning,MySQL'
mysql> SELECT MAKE_SET(1|4, 'Learning', 'MySQL', 'is', 'fun');
Result: 'Learning,is'
mysql> SELECT MAKE_SET(1|6, 'Learning', 'MySQL', 'is', 'fun');
Result: 'Learning,MySQL,is'
mysql> SELECT MAKE_SET(1|8, 'Learning', 'MySQL', 'is', 'fun');
Result: 'Learning,fun'
mysql> SELECT MAKE_SET(1|14, 'Learning', 'MySQL', 'is', 'fun');
Result: 'Learning,MySQL,is,fun'
mysql> SELECT MAKE_SET(15, 'Learning', 'MySQL', 'is', 'fun');
Result: 'Learning,MySQL,is,fun'
mysql> SELECT MAKE_SET(15, 'A', 'B', NULL, 'D');
Result: 'A,B,D'
示例 2:
考虑一个名为 Employee 的数据库表,其中包含以下记录:
EmpID | Name | Department1 | Department2 | Department3 |
---|---|---|---|---|
1 | John | HR | Admin | Treasury |
2 | Marry | IT | Finance | |
3 | Jo | Finance | Marketing | |
4 | Kim | Marketing | Sales | |
5 | Ramesh | IT | Finance | Marketing |
6 | Huang | IT | Marketing | Sales |
下面给出的语句可用于获取集合由位集中的位指定的值。
/* 28的二进制为11100 */
SELECT *,
MAKE_SET(28, EmpID, Name, Department1, Department2, Department3) AS MAKE_SET_Value
FROM Employee;
这将产生如下所示的结果:
EmpID | Name | Department1 | Department2 | Department3 | MAKE_SET_Value |
---|---|---|---|---|---|
1 | John | HR | Admin | Treasury | HR,Admin,Treasury |
2 | Marry | IT | Finance | IT,Finance | |
3 | Jo | Finance | Marketing | Finance,Marketing | |
4 | Kim | Marketing | Sales | Marketing,Sales | |
5 | Ramesh | IT | Finance | Marketing | IT,Finance,Marketing |
6 | Huang | IT | Marketing | Sales | IT,Marketing,Sales |