search
Search
Login
Unlock 100+ guides
menu
menu
web
search toc
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to

MySQL | MAKE_SET method

schedule Aug 12, 2023
Last updated
local_offer
MySQL
Tags
tocTable of Contents
expand_more
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
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 is 0, 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 is 0, 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 is 1, 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.

robocat
Published by Arthur Yanagisawa
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!