跳到主要内容

FOREACH

FOREACH

description

Syntax

AGGREGATE_FUNCTION_FOREACH(arg...) 将表的聚合函数转换为聚合相应数组项并返回结果数组的数组的聚合函数。 例如, sum_foreach 对于数组 [1, 2], [3, 4, 5]和[6, 7]返回结果 [10, 13, 5] 之后将相应的数组项添加在一起。

example

mysql [test]>select a , s from db;
+-----------+---------------+
| a | s |
+-----------+---------------+
| [1, 2, 3] | ["ab", "123"] |
| [20] | ["cd"] |
| [100] | ["efg"] |
| NULL | NULL |
| [null, 2] | [null, "c"] |
+-----------+---------------+

mysql [test]>select sum_foreach(a) from db;
+----------------+
| sum_foreach(a) |
+----------------+
| [121, 4, 3] |
+----------------+

mysql [test]>select count_foreach(s) from db;
+------------------+
| count_foreach(s) |
+------------------+
| [3, 2] |
+------------------+

mysql [test]>select array_agg_foreach(a) from db;
+-----------------------------------+
| array_agg_foreach(a) |
+-----------------------------------+
| [[1, 20, 100, null], [2, 2], [3]] |
+-----------------------------------+

mysql [test]>select map_agg_foreach(a,a) from db;
+---------------------------------------+
| map_agg_foreach(a, a) |
+---------------------------------------+
| [{1:1, 20:20, 100:100}, {2:2}, {3:3}] |
+---------------------------------------+

keywords

FOREACH