> ## Documentation Index
> Fetch the complete documentation index at: /documents/llms.txt
> Use this file to discover all available pages before exploring further.

# arrays_zip

> 根据元素位置将给定的数组合并为 Struct 数组，其中第 n 个 Struct 包含所有输入数组的第 n 个值。

根据元素位置合并给定的数组，将其合并为一个 Struct 数组，其中第 n 个 Struct 包含所有输入数组的第 n 个值。当数组长度不同时，缺失的元素用 NULL 填充。

## 语法

```sql theme={null}
arrays_zip(array_1, array_2[, ...])
```

## 参数

* `array_n`：要合并的数组。每个数组可以包含任何支持类型的元素。

## 返回值

返回一个 Struct 的数组，其中第 n 个 Struct 包含每个输入数组的第 n 个字段。如果输入数组长度不同，较短的数组用 NULL 值填充。

## 使用说明

* 如果输入数组长度不同，结果数组的长度为输入数组中最长的长度。
* 较短数组中的缺失值用 NULL 填充。
* 如果任意输入数组为 NULL，结果为 NULL。
* 如果所有输入数组为空，结果为一个空数组。

## 示例

示例 1：合并两个长度相等的数组。

```sql theme={null}
mysql> SELECT arrays_zip([1, 2], ['a', 'b']) AS result;
+---------------------------------------------------+
| result                                            |
+---------------------------------------------------+
| [{"col1":"1","col2":"a"},{"col1":"2","col2":"b"}] |
+---------------------------------------------------+
```

示例 2：合并两个长度不同的数组。

```sql theme={null}
mysql> SELECT arrays_zip([1, 2], ['a', null, 'c']) AS result;
+-----------------------------------------------------------------------------+
| result                                                                      |
+-----------------------------------------------------------------------------+
| [{"col1":"1","col2":"a"},{"col1":"2","col2":null},{"col1":null,"col2":"c"}] |
+-----------------------------------------------------------------------------+
```

示例 3：合并三个数组。

```sql theme={null}
mysql> SELECT arrays_zip([1, 2, 3], ['a', 'b', 'c'], [10, 20, 30]) AS result;
+---------------------------------------------------------------------------------------------------------------+
| result                                                                                                        |
+---------------------------------------------------------------------------------------------------------------+
| [{"col1":"1","col2":"a","col3":"10"},{"col1":"2","col2":"b","col3":"20"},{"col1":"3","col2":"c","col3":"30"}] |
+---------------------------------------------------------------------------------------------------------------+
```

示例 4：合并两个空数组。

```sql theme={null}
mysql> SELECT arrays_zip([], []) AS result;
+--------+
| result |
+--------+
| []     |
+--------+
```

示例 5：合并单个数组。

```sql theme={null}
mysql> SELECT arrays_zip([1, 2, 3]) AS result;
+------------------------------------------+
| result                                   |
+------------------------------------------+
| [{"col1":"1"},{"col1":"2"},{"col1":"3"}] |
+------------------------------------------+
```

示例 6：合并其中一个为 NULL 的数组。

```sql theme={null}
mysql> SELECT arrays_zip(null, [1, 2]) AS result;
+--------+
| result |
+--------+
| NULL   |
+--------+
```

示例 7：合并包含 NULL 值的混合类型数组。

```sql theme={null}
mysql> SELECT arrays_zip([1, null, 3], ['a', 'b', null]) AS result;
+-----------------------------------------------------------------------------+
| result                                                                      |
+-----------------------------------------------------------------------------+
| [{"col1":"1","col2":"a"},{"col1":null,"col2":"b"},{"col1":"3","col2":null}] |
+-----------------------------------------------------------------------------+
```

示例 8：从表数据中合并数组。

```sql theme={null}
mysql> CREATE TABLE IF NOT EXISTS test (id INT, arr1 ARRAY<INT>, arr2 ARRAY<VARCHAR>) PROPERTIES ("replication_num"="1");
mysql> INSERT INTO test VALUES (1, [1, 2], ['a', 'b']), (2, [3, 4, 5], ['x', 'y']);
mysql> SELECT id, arrays_zip(arr1, arr2) AS result FROM test;
+------+----------------------------------------------------------------------------+
| id   | result                                                                     |
+------+----------------------------------------------------------------------------+
|    1 | [{"col1":"1","col2":"a"},{"col1":"2","col2":"b"}]                          |
|    2 | [{"col1":"3","col2":"x"},{"col1":"4","col2":"y"},{"col1":"5","col2":null}] |
+------+----------------------------------------------------------------------------+
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.