Categories: MySQL FunctionsPHP

MySQL8 MAKE_SET() Functions – String Functions

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.


MySQL MAKE_SET() Functions: Syntax

MAKE_SET ( bits, string1,  string2, … … … …. ….);

MySQL MAKE_SET() Functions: Parameter

[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]


MySQL MAKE_SET() Functions: Available from

[table caption=”” width=”100%” colwidth=”25%|75%” colalign=”left|left”]
Version, MySQL 5.7
[/table]


MAKE_SET() Example 1 : Basic Usage

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.

  1. 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.
  2. The second bit starting from the right of the binary number shown is 1 (or on), so the second string starting from the right of the list of strings is put into the set.
  3. The third bit starting from the right of the binary number shown is 0 (or off), so the third string starting from the right of the list of strings is not put into the set.
  4. The fourth bit counting from the right is 1, so the fourth string of the list of strings is added to the set.

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.


MAKE_SET() Example 2 : Some more examples

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)

LOCATE() Example 3 : Multiple Binary Values

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)


LIKE() Example 4 : NULL arguments

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.


Related articles : HEX(), CONCAT(), CONCAT_WS() , LOWER(), LTRIM(), INSTR(), POSITION().


PHP Related articles : SUBSTR(), SUBSTR_COUNT(), SUBSTR_COMPARE(), SUBSTR_REPLACE(), PHP STRING FUNCTIONS().

jyoti rani

Recent Posts

What Is a Progressive Web App? Why Would You Need One?

App usage is growing steadily without showing any signs of slowing down. Hence, it is no surprise that mobile applications…

1 year ago

7 Most Popular Paid Online Advertising Strategy

As the world has grown more digital, businesses have adapted themselves. An effectual adaptation includes online advertising. Offline advertising styles…

1 year ago

The Importance of User-Centered Design in Mobile App Development

Step into a world where apps dance to the user's tune. Picture Instagram, a photo-sharing sensation that swept the globe.…

1 year ago

Healthcare Mobile App Development: A Complete Guide for Founders

COVID-19 has led to a digitalization of lifestyle. As patients are taking their mental and physical health more seriously, healthcare…

1 year ago

Exploring Diverse WordPress Theme Niches: A Comprehensive Guide

Introduction WordPress, an immensely popular content management system (CMS), powers over 40% of the internet. What makes WordPress even more…

1 year ago

8 Awesome Blog Content Ideas for Movers to Skyrocket the SEO

For moving companies trying to capture their market share amidst stiff competition, a tip or two about what they can…

1 year ago