MySQL MAKE_SET Function

MySQL MAKE_SET function returns a set value that contains substrings separated by a comma character. This MAKE_SET function uses bits to form or make set values.

Let me show you how to write a MySQL MAKE_SET String Function query to make or create a set value using binary bits or bit values with an example. The basic syntax of MAKE_SET of a string is as shown below:

MAKE_SET(bits, Str1, Str2,...StrnN)

MySQL MAKE_SET Function Example

The MySQL string MAKE_SET function makes a set using the bit values of substrings. The following query shows multiple ways to use this method. Here, bits = 1 = 0001. So, 1 = a, 0 = b, 0 = c, 0 = d. It means the output is a. Within the third statement, bits = 2 = 0010 (0 = a, 1 = b, 0 = c, 0 = d). So, it returns b

SELECT MAKE_SET(0, 'a', 'b', 'c', 'd');

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

SELECT MAKE_SET(2, 'a', 'b', 'c', 'd');
MAKE_SET Example 1

In this MySQL Make_Set example, we are using multiple-bit values. First, 1|3, bits 1 = 00001 means, 1= hi, 0 = hello, 0 = mysl, 0 = world, 0 = Welcome. Next, 3 = 00011. So, the String Function output will be hi, hello.

Within the third MySQL statement, we used 2|4. bits 2 = 00010 => hello. bits 4 = 00100 => 0= hi, 0 = hello, 1 = msql, 0 = world, 0 = Welcome.

SELECT MAKE_SET(1 | 3, 'hi', 'hello', 'mysql', 'world', 'welcome');

SELECT MAKE_SET(1 | 4, 'hi', 'hello', 'mysql', 'world', 'welcome');

SELECT MAKE_SET(2 | 4, 'hi', 'hello', 'mysql', 'world', 'welcome');
MySQL MAKE_SET Function 2

About Suresh

Suresh is the founder of TutorialGateway and a freelance software developer. He specialized in Designing and Developing Windows and Web applications. The experience he gained in Programming and BI integration, and reporting tools translates into this blog. You can find him on Facebook or Twitter.