MySQL EXPORT_SET() Function
In MySQL, the EXPORT_SET() function generates a string using the specified delimiter based on the bits of the argument.
EXPORT_SET() Syntax
Here is the syntax of MySQL EXPORT_SET() function:
EXPORT_SET(bits, on, off, separator, length)
Parameters
bits- Required. A number. Converting
bitsto binary and inverting the value of each bit and check each bit. on- Required. THe string to use if the bit value is
1. off- Required. THe string to use if the bit value is
0. separator- Optional. Delimiter or delimited string, the default value is
,. length- Optional. The number of elements in the set, the default value is
64.
Return value
The EXPORT_SET() function picks the corresponding string according to the bit value, and returns a comma-separated set of strings.
Let’s use EXPORT_SET(5, 'Aa', 'Bb', '#', 4) as an example:
-
bits = 5, the binary bits of5is101, the total length is4, then left pad to lenth 4 and it becomes0101. Then reverse it to1010. -
Use
onoroffby each bit in1010:- The first bit is
1, so we useAa. - The second bit is
0, so we useBb. - The third bit is
1, so we useAa. - The fourth bit is
0, so we useBb.
- The first bit is
-
Finally, join all strings from previous step with ‘#’, ang get it:
Aa#Bb#Aa#Bb.
EXPORT_SET() Examples
SELECT
EXPORT_SET(5, 'Aa', 'Bb', '#', 4),
EXPORT_SET(5, 'Y', 'N', ',', 4),
EXPORT_SET(5, '1', '0', ',', 10),
EXPORT_SET(0, 'Y', 'N', ',', 4)\G
EXPORT_SET(5, 'Aa', 'Bb', '#', 4): Aa#Bb#Aa#Bb
EXPORT_SET(5, 'Y', 'N', ',', 4): Y,N,Y,N
EXPORT_SET(5, '1', '0', ',', 10): 1,0,1,0,0,0,0,0,0,0
EXPORT_SET(0, 'Y', 'N', ',', 4): N,N,N,N