array_join

SinceVersion 1.2.0

array_join

description

Syntax

1. VARCHAR array_join(ARRAY<T> arr, VARCHAR sep[, VARCHAR null_replace])

Combines all elements in the array to generate a new string according to the separator (sep) and the string to replace NULL (null_replace). If sep is NULL, return NULL. If null_replace is NULL, return NULL. If sep is an empty string, no delimiter is applied. If null_replace is an empty string or not specified, the NULL elements in the array are discarded directly.

notice

Only supported in vectorized engine

example

1. mysql> set enable_vectorized_engine=true;

3. mysql> select k1, k2, array_join(k2, '_', 'null') from array_test order by k1;
4. +------+-----------------------------+------------------------------------+
5. | k1   | k2                          | array_join(`k2`, '_', 'null')      |
6. +------+-----------------------------+------------------------------------+
7. |  1   | [1, 2, 3, 4, 5]             | 1_2_3_4_5                          |
8. |  2   | [6, 7, 8]                   | 6_7_8                              |
9. |  3   | []                          |                                    |
10. |  4   | NULL                        | NULL                               |
11. |  5   | [1, 2, 3, 4, 5, 4, 3, 2, 1] | 1_2_3_4_5_4_3_2_1                  |
12. |  6   | [1, 2, 3, NULL]             | 1_2_3_null                         |
13. |  7   | [4, 5, 6, NULL, NULL]       | 4_5_6_null_null                    |
14. +------+-----------------------------+------------------------------------+

16. mysql> select k1, k2, array_join(k2, '_', 'null') from array_test01 order by k1;
17. +------+-----------------------------------+------------------------------------+
18. | k1   | k2                                | array_join(`k2`, '_', 'null')      |
19. +------+-----------------------------------+------------------------------------+
20. |  1   | ['a', 'b', 'c', 'd']              | a_b_c_d                            |
21. |  2   | ['e', 'f', 'g', 'h']              | e_f_g_h                            |
22. |  3   | [NULL, 'a', NULL, 'b', NULL, 'c'] | null_a_null_b_null_c               |
23. |  4   | ['d', 'e', NULL, ' ']             | d_e_null_                          |
24. |  5   | [' ', NULL, 'f', 'g']             |  _null_f_g                         |
25. +------+-----------------------------------+------------------------------------+

27. mysql> select k1, k2, array_join(k2, '_') from array_test order by k1;
28. +------+-----------------------------+----------------------------+
29. | k1   | k2                          | array_join(`k2`, '_')      |
30. +------+-----------------------------+----------------------------+
31. |  1   | [1, 2, 3, 4, 5]             | 1_2_3_4_5                  |
32. |  2   | [6, 7, 8]                   | 6_7_8                      |
33. |  3   | []                          |                            |
34. |  4   | NULL                        | NULL                       |
35. |  5   | [1, 2, 3, 4, 5, 4, 3, 2, 1] | 1_2_3_4_5_4_3_2_1          |
36. |  6   | [1, 2, 3, NULL]             | 1_2_3                      |
37. |  7   | [4, 5, 6, NULL, NULL]       | 4_5_6                      |
38. +------+-----------------------------+----------------------------+

40. mysql> select k1, k2, array_join(k2, '_') from array_test01 order by k1;
41. +------+-----------------------------------+----------------------------+
42. | k1   | k2                                | array_join(`k2`, '_')      |
43. +------+-----------------------------------+----------------------------+
44. |  1   | ['a', 'b', 'c', 'd']              | a_b_c_d                    |
45. |  2   | ['e', 'f', 'g', 'h']              | e_f_g_h                    |
46. |  3   | [NULL, 'a', NULL, 'b', NULL, 'c'] | a_b_c                      |
47. |  4   | ['d', 'e', NULL, ' ']             | d_e_                       |
48. |  5   | [' ', NULL, 'f', 'g']             |  _f_g                      |
49. +------+-----------------------------------+----------------------------+

keywords

ARRAY, JOIN, ARRAY_JOIN