MySQL | MAKE_SET method
Start your free 7-days trial now!
MySQL's MAKE_SET(~) method returns a set of comma-separated substrings that have the corresponding bit in the provided bit value(s).
Parameters
1. bits | bits
A set of bits that specifies which strings are returned in the returned comma-separated substring.
2. str1 | string
A string to be appended to the return string if it has the corresponding bit specified in bits. Any number of strings may be provided.
Examples
Basic explanation
To return a string based on bits arguments 4:
SELECT MAKE_SET(4, 'a', 'b', 'c');
+-------------------------+| MAKE_SET(4,'a','b','c') |+-------------------------+| c |+-------------------------+
Representing 4 in binary is 100. Reading the binary from the right:
The first bit controls whether the first string
'a'is appended to the return value. As the first bit is0, we do not append'a'to the return string.The second bit controls whether the second string
'b'is appended to the return value. Again as the bit is0, we do not append'b'to the return string.Finally the third bit controls whether the third string
'c'is appended to the return value. As the third bit is1, we do append'c'to the return string. Indeed we see our return value is simply'c'.
Multiple bits
In the first example we only provided a single bits value, but we can use a pipe to specify a set of multiple bits:
SELECT MAKE_SET(2 | 4, 'a', 'b', 'c', 'd', 'e', 'f');
+-----------------------------------------+| MAKE_SET(2 | 4,'a','b','c','d','e','f') |+-----------------------------------------+| b,c |+-----------------------------------------+
Binary of 2: 10
Binary of 4: 100
We return the second and third strings from the provided set of string arguments as the second bit of binary of 2 is a set bit ('1') and the third bit of binary of 4 is a set bit. Notice the returned strings are comma separated.