MAKE_SET() Functions returns a comma-separated list of values that match with bits of a base 10 number converted to its binary equivalent.
When we call the function, we specify any number of strings (separated by a comma), as well as one or more bit values that determine which strings to return in the set value.
string1 corresponds to bit 0, string2 to bit 1, and so on.
NULL values in str1, str2, .. are not appended to the result. See example 4.
[table caption=”” width=”100%” colwidth=”15%|15%|15%|55%” colalign=”left|left|left|left”]
Name, Required /Optional,Type, Description
string, Required, String , It represents any number of strings separated by a comma.
bits, Required, String , It represents one or more bit values that determine which strings to return in the set value.
[/table]
[table caption=”” width=”100%” colwidth=”25%|75%” colalign=”left|left”]
Version, MySQL 5.7
[/table]
Below is the example to show it. Here we have comma-separated list of values and bit values that determine which strings to return in the set value.
mysql> SELECT BIN(11) AS 'Binary 11',MAKE_SET(11, 'A','B','C','D'); +-----------+-------------------------------+ | Binary 11 | MAKE_SET(11, 'A','B','C','D') | +-----------+-------------------------------+ | 1011 | A,B,D | +-----------+-------------------------------+ 1 row in set (0.00 sec))
Note : that the binary equivalent of 11 is 1011.
Example 2 :
mysql> SELECT BIN(7) AS 'Binary 1',MAKE_SET(7, 'A','B','C','D'); +----------+------------------------------+ | Binary 1 | MAKE_SET(7, 'A','B','C','D') | +----------+------------------------------+ | 111 | A,B,C | +----------+------------------------------+ 1 row in set (0.00 sec)
Note : The binary equivalent of 7 is 111. The first bit starting from the right of the binary number shown is 1 (or on), so the first string starting from the right of the list of strings is put into the set. The second and third bits of the binary number are also 1, so the second and third strings (i.e., B and C) are added to the set.
Below are the example to show some more examples.
mysql> SELECT BIN(4) AS 'Binary 4',MAKE_SET(4, 'A','B','C','D'); +----------+------------------------------+ | Binary 4 | MAKE_SET(4, 'A','B','C','D') | +----------+------------------------------+ | 100 | C | +----------+------------------------------+ 1 row in set (0.00 sec) mysql> SELECT BIN(8) AS 'Binary 8',MAKE_SET(1000, 'A','B','C','D'); +----------+---------------------------------+ | Binary 8 | MAKE_SET(1000, 'A','B','C','D') | +----------+---------------------------------+ | 1000 | D | +----------+---------------------------------+ 1 row in set (0.00 sec)
You can use a pipe in order to pass multiple binary values in the first argument:
mysql> SELECT BIN(4|2),MAKE_SET( 4 | 2 , 'A','B','C','D'); +----------+------------------------------------+ | BIN(4|2) | MAKE_SET( 4 | 2 , 'A','B','C','D') | +----------+------------------------------------+ | 110 | B,C | +----------+------------------------------------+ 1 row in set (0.00 sec)
Note that you’ll get the same result if you reverse the binary values in the first argument:
mysql> SELECT BIN(2|4),MAKE_SET( 2 | 4 , 'A','B','C','D'); +----------+------------------------------------+ | BIN(2|4) | MAKE_SET( 2 | 4 , 'A','B','C','D') | +----------+------------------------------------+ | 110 | B,C | +----------+------------------------------------+ 1 row in set (0.00 sec)
Any strings with NULL values are not appended to the result. See below example
mysql> SELECT BIN(2|4),MAKE_SET( 2 | 4 , 'A',NULL,'C','D'); +----------+-------------------------------------+ | BIN(2|4) | MAKE_SET( 2 | 4 , 'A',NULL,'C','D') | +----------+-------------------------------------+ | 110 | C | +----------+-------------------------------------+ 1 row in set (0.05 sec)
See all MySQL String functions MySQL 8 String Functions.
App usage is growing steadily without showing any signs of slowing down. Hence, it is no surprise that mobile applications…
As the world has grown more digital, businesses have adapted themselves. An effectual adaptation includes online advertising. Offline advertising styles…
Step into a world where apps dance to the user's tune. Picture Instagram, a photo-sharing sensation that swept the globe.…
COVID-19 has led to a digitalization of lifestyle. As patients are taking their mental and physical health more seriously, healthcare…
Introduction WordPress, an immensely popular content management system (CMS), powers over 40% of the internet. What makes WordPress even more…
For moving companies trying to capture their market share amidst stiff competition, a tip or two about what they can…