跳到主要内容
版本: 最新版本-3.5

array_position

获取元素在数组中的位置。

语法

array_position(any_array, any_element)

参数

  • any_array:要搜索的数组。
  • any_element:与数组中的元素匹配的表达式。

返回值

如果在指定数组中找到指定元素,则返回该元素的位置;如果在数组中找不到该元素,则返回 0。

示例

  • 示例 1:获取数组中元素的位置。
mysql> select array_position(["apple","orange","pear"], "orange");

+-----------------------------------------------------+

| array_position(['apple','orange','pear'], 'orange') |

+-----------------------------------------------------+

| 2 |

+-----------------------------------------------------+

1 row in set (0.01 sec)
  • 示例 2:获取数组中 NULL 的位置。
mysql> select array_position([1, NULL], NULL);
+--------------------------------+

| array_position([1,NULL], NULL) |

+--------------------------------+

| 2 |

+--------------------------------+

1 row in set (0.00 sec)
  • 示例 3:获取多维数组中子数组的位置。 只有在多维数组中找到包含与指定子数组相同元素且顺序相同的子数组时,才能返回位置。
mysql> select array_position([[1,2,3], [4,5,6]], [4,5,6]);

+--------------------------------------------+

| array_position([[1,2,3],[4,5,6]], [4,5,6]) |

+--------------------------------------------+

| 2 |

+--------------------------------------------+

1 row in set (0.00 sec)



mysql> select array_position([[1,2,3], [4,5,6]], [4,6,5]);

+--------------------------------------------+

| array_position([[1,2,3],[4,5,6]], [4,6,5]) |

+--------------------------------------------+

| 0 |

+--------------------------------------------+

1 row in set (0.00 sec)

关键词

ARRAY_POSITION, ARRAY