w3resource
MYSQL Tutorial

MySQL MAKE_SET() function

MAKE_SET() function

MySQL MAKE_SET() returns a set value (a string containing substrings separated by “,” characters) consisting of the strings that have the corresponding bit in the first argument.

Syntax:

MAKE_SET (bits, str1, str2,….)

Arguments

Name Description
bits An expression.
str1, str2,…. A list of strings.

MySQL Version: 5.6

Video Presentation

Pictorial Presentation

MySQL MAKE_SET() pictorial presentation

Example -1: MySQL MAKE_SET() function

Code:

SELECT MAKE_SET(1,'a','b','c');

Explanation

If the first argument (1) is converted to binary, it returns 1. So for the rightmost bit, the function returns 'a'. Since no other bits (obtained from the first argument) are available, the function does not add anything with 'a'.So the final output is 'a' .

Sample Output:

mysql> SELECT MAKE_SET(1,'a','b','c');
+-------------------------+
| MAKE_SET(1,'a','b','c') |
+-------------------------+
| a                       | 
+-------------------------+
1 row in set (0.00 sec)

Example -2 : MySQL MAKE_SET() function

Code:

SELECT MAKE_SET(1 | 4,'hello','nice','world');

Explanation

If the first argument (1 | 4) is converted to binary, it returns 1 or 100. For 1, the rightmost bit is 1, so the function returns 'hello'. For 100, the rightmost bit is 0, so the function returns nothing for the rightmost bit (0). But the function returns 'world' for the leftmost bit (1). So, the final output is hello,world.

Sample Output:

mysql> SELECT MAKE_SET(1 | 4,'hello','nice','world'); 
+----------------------------------------+
| MAKE_SET(1 | 4,'hello','nice','world') |
+----------------------------------------+
| hello,world                            | 
+----------------------------------------+
1 row in set (0.00 sec)

All String Functions

MySQL String Functions, slide presentation

Many of our valued users post comments along with piece of code. Disqus may, false positively, consider those piece of code as Bad or Starnge syntax and send those comments to spam automatically. Even if that happens, we will make sure those useful comments are taken out of spam and approved. It may take a couple of days for that though, but any useful comment will be brought to public view for sure. We regret if that happend to any user. You may write us directly regarding this to - w3resource[at]yahoo[dot]com.