array_exists

SinceVersion 2.0

array_exists(lambda,array1,array2….) array_exists(array1)

description

Syntax

1. BOOLEAN array_exists(lambda, ARRAY<T> arr1, ARRAY<T> arr2, ... )
2. BOOLEAN array_exists(ARRAY<T> arr)

使用一个可选lambda表达式作为输入参数,对其他的输入ARRAY参数的内部数据做对应表达式计算。当计算返回非0时,返回1;否则返回0。 在lambda表达式中输入的参数为1个或多个,必须和后面的输入array列数量一致。在lambda中可以执行合法的标量函数,不支持聚合函数等。 在没有使用lambda作为参数时,array1作为计算结果。

1. array_exists(x->x, array1);
2. array_exists(x->(x%2 = 0), array1);
3. array_exists(x->(abs(x)-1), array1);
4. array_exists((x,y)->(x = y), array1, array2);
5. array_exists(array1);

example

2. mysql [test]>select *, array_exists(x->x>1,[1,2,3]) from array_test2 order by id;
3. +------+-----------------+-------------------------+-----------------------------------------------+
4. | id   | c_array1        | c_array2                | array_exists([x] -> x(0) > 1, ARRAY(1, 2, 3)) |
5. +------+-----------------+-------------------------+-----------------------------------------------+
6. |    1 | [1, 2, 3, 4, 5] | [10, 20, -40, 80, -100] | [0, 1, 1]                                     |
7. |    2 | [6, 7, 8]       | [10, 12, 13]            | [0, 1, 1]                                     |
8. |    3 | [1]             | [-100]                  | [0, 1, 1]                                     |
9. |    4 | NULL            | NULL                    | [0, 1, 1]                                     |
10. +------+-----------------+-------------------------+-----------------------------------------------+
11. 4 rows in set (0.02 sec)

13. mysql [test]>select c_array1, c_array2, array_exists(x->x%2=0,[1,2,3]) from array_test2 order by id;
14. +-----------------+-------------------------+---------------------------------------------------+
15. | c_array1        | c_array2                | array_exists([x] -> x(0) % 2 = 0, ARRAY(1, 2, 3)) |
16. +-----------------+-------------------------+---------------------------------------------------+
17. | [1, 2, 3, 4, 5] | [10, 20, -40, 80, -100] | [0, 1, 0]                                         |
18. | [6, 7, 8]       | [10, 12, 13]            | [0, 1, 0]                                         |
19. | [1]             | [-100]                  | [0, 1, 0]                                         |
20. | NULL            | NULL                    | [0, 1, 0]                                         |
21. +-----------------+-------------------------+---------------------------------------------------+
22. 4 rows in set (0.02 sec)

24. mysql [test]>select c_array1, c_array2, array_exists(x->abs(x)-1,[1,2,3]) from array_test2 order by id;
25. +-----------------+-------------------------+----------------------------------------------------+
26. | c_array1        | c_array2                | array_exists([x] -> abs(x(0)) - 1, ARRAY(1, 2, 3)) |
27. +-----------------+-------------------------+----------------------------------------------------+
28. | [1, 2, 3, 4, 5] | [10, 20, -40, 80, -100] | [0, 1, 1, 1, 1]                                    |
29. | [6, 7, 8]       | [10, 12, 13]            | [1, 1, 1]                                          |
30. | [1, NULL]       | [-100]                  | [0, NULL]                                          |
31. | NULL            | NULL                    | NULL                                               |
32. +-----------------+-------------------------+----------------------------------------------------+
33. 4 rows in set (0.02 sec)

35. mysql [test]>select c_array1, c_array2, array_exists((x,y)->x>y,c_array1,c_array2) from array_test2 order by id;
36. +-----------------+-------------------------+-------------------------------------------------------------+
37. | c_array1        | c_array2                | array_exists([x, y] -> x(0) > y(1), `c_array1`, `c_array2`) |
38. +-----------------+-------------------------+-------------------------------------------------------------+
39. | [1, 2, 3, 4, 5] | [10, 20, -40, 80, -100] | [0, 0, 1, 0, 1]                                             |
40. | [6, 7, 8]       | [10, 12, 13]            | [0, 0, 0]                                                   |
41. | [1]             | [-100]                  | [1]                                                         |
42. | NULL            | NULL                    | NULL                                                        |
43. +-----------------+-------------------------+-------------------------------------------------------------+
44. 4 rows in set (0.02 sec)

46. mysql [test]>select *, array_exists(c_array1) from array_test2 order by id;
47. +------+-----------------+-------------------------+--------------------------+
48. | id   | c_array1        | c_array2                | array_exists(`c_array1`) |
49. +------+-----------------+-------------------------+--------------------------+
50. |    1 | [1, 2, 3, 0, 5] | [10, 20, -40, 80, -100] | [1, 1, 1, 0, 1]          |
51. |    2 | [6, 7, 8]       | [10, 12, 13]            | [1, 1, 1]                |
52. |    3 | [0, NULL]       | [-100]                  | [0, NULL]                |
53. |    4 | NULL            | NULL                    | NULL                     |
54. +------+-----------------+-------------------------+--------------------------+
55. 4 rows in set (0.02 sec)

keywords

ARRAY,ARRAY_EXISTS