跳到主要内容

数组输出函数

数组输出函数用于选择数组数据输出形式,如以字符串的形式依次输出数组数据的每一个元素,并以逗号隔开。seekdb 当前所支持的数组输出函数包括 array_to_string()

array_to_string

array_to_string() 函数将数组转换为字符串。具体而言,即根据指定的分隔符和空元素符将数组中的所有基础元素打印成字符串。如果数组为嵌套数组,则递归打印其非空子数组。与 string_to_array() 不完全互为逆函数。语法如下:

array_to_string(arr1, delimiter[, null_str])

输入参数说明如下:

  • arr1 传入的类型必须是一个数组类型。
  • delimiter 分隔符,支持字符类型,包含 Char、Varchar 类型。
  • null_str(可选) 空元素符,支持字符类型,包含 Char、Varchar 类型。若不指定,则不打印 NULL 元素及其相应的分隔符。

返回结果为 Text 类型的字符串。

示例如下:

SELECT array_to_string([1,2,NULL,3], '-', 'N');
+-----------------------------------------+
| array_to_string([1,2,NULL,3], '-', 'N') |
+-----------------------------------------+
| 1-2-N-3 |
+-----------------------------------------+
1 row in set (0.001 sec)
SELECT array_to_string([1,2,NULL,3], 'and');
+--------------------------------------+
| array_to_string([1,2,NULL,3], 'and') |
+--------------------------------------+
| 1and2and3 |
+--------------------------------------+
1 row in set (0.001 sec)
SELECT array_to_string([["hello", "world"], ["hi", "what"]], '-');
+------------------------------------------------------------+
| array_to_string([["hello", "world"], ["hi", "what"]], '-') |
+------------------------------------------------------------+
| hello-world-hi-what |
+------------------------------------------------------------+
1 row in set (0.001 sec)
SELECT array_to_string([["hello", "world"], ["hi", "what"], NULL], '-', 'N');
+-----------------------------------------------------------------------+
| array_to_string([["hello", "world"], ["hi", "what"], NULL], '-', 'N') |
+-----------------------------------------------------------------------+
| hello-world-hi-what-N |
+-----------------------------------------------------------------------+
1 row in set (0.001 sec)