FOREACH

description

Syntax

AGGREGATE_FUNCTION_FOREACH(arg...) Converts an aggregate function for tables into an aggregate function for arrays that aggregates the corresponding array items and returns an array of results. For example, sum_foreach for the arrays [1, 2], [3, 4, 5]and[6, 7]returns the result [10, 13, 5] after adding together the corresponding array items.

example

  1. mysql [test]>select a , s from db;
  2. +-----------+---------------+
  3. | a | s |
  4. +-----------+---------------+
  5. | [1, 2, 3] | ["ab", "123"] |
  6. | [20] | ["cd"] |
  7. | [100] | ["efg"] |
  8. | NULL | NULL |
  9. | [null, 2] | [null, "c"] |
  10. +-----------+---------------+
  11. mysql [test]>select sum_foreach(a) from db;
  12. +----------------+
  13. | sum_foreach(a) |
  14. +----------------+
  15. | [121, 4, 3] |
  16. +----------------+
  17. mysql [test]>select count_foreach(s) from db;
  18. +------------------+
  19. | count_foreach(s) |
  20. +------------------+
  21. | [3, 2] |
  22. +------------------+
  23. mysql [test]>select array_agg_foreach(a) from db;
  24. +-----------------------------------+
  25. | array_agg_foreach(a) |
  26. +-----------------------------------+
  27. | [[1, 20, 100, null], [2, 2], [3]] |
  28. +-----------------------------------+
  29. mysql [test]>select map_agg_foreach(a,a) from db;
  30. +---------------------------------------+
  31. | map_agg_foreach(a, a) |
  32. +---------------------------------------+
  33. | [{1:1, 20:20, 100:100}, {2:2}, {3:3}] |
  34. +---------------------------------------+

keywords

FOREACH