得到交集, $2为真子集则返回true

  1. create or replace function array_intersect(anyarray, anyarray)
  2. returns anyarray
  3. immutable
  4. language sql
  5. as $function$
  6. select array(
  7. select unnest($1)
  8. intersect
  9. select unnest($2)
  10. );
  11. $function$;
  12. create or replace function array_intersect(text[], text)
  13. returns anyarray
  14. immutable
  15. language sql
  16. as $function$
  17. select array_intersect($1,string_to_array($2,','))
  18. $function$;
  19. create or replace function array_intersect(int[], text)
  20. returns anyarray
  21. immutable
  22. language sql
  23. as $function$
  24. select array_intersect($1,string_to_array($2,',')::int[])
  25. $function$;
  26. create or replace function array_intersect(bigint[], text)
  27. returns anyarray
  28. immutable
  29. language sql
  30. as $function$
  31. select array_intersect($1,string_to_array($2,',')::bigint[])
  32. $function$;
  33. create or replace function array_has_intersection(anyarray, anyarray)
  34. returns bool
  35. immutable
  36. language sql
  37. as $function$
  38. select true from (
  39. select unnest($1)
  40. intersect
  41. select unnest($2)
  42. ) t limit 1;
  43. $function$;
  44. create or replace function array_has_intersection(bigint[], text[])
  45. returns bool
  46. immutable
  47. language sql
  48. as $function$
  49. select array_has_intersection($1,$2::bigint[])
  50. $function$;
  51. create or replace function array_has_intersection(bigint[], int[])
  52. returns bool
  53. immutable
  54. language sql
  55. as $function$
  56. select array_has_intersection($1,$2::bigint[])
  57. $function$;
  58. create or replace function array_has_intersection(int[], text[])
  59. returns bool
  60. immutable
  61. language sql
  62. as $function$
  63. select array_has_intersection($1,$2::int[])
  64. $function$;
  65. create or replace function array_has_intersection(text[], text)
  66. returns bool
  67. immutable
  68. language sql
  69. as $function$
  70. select array_has_intersection($1,string_to_array($2,','))
  71. $function$;
  72. create or replace function array_has_intersection(int[], text)
  73. returns bool
  74. immutable
  75. language sql
  76. as $function$
  77. select array_has_intersection($1,string_to_array($2,',')::int[])
  78. $function$;
  79. create or replace function array_has_intersection(bigint[], text)
  80. returns bool
  81. immutable
  82. language sql
  83. as $function$
  84. select array_has_intersection($1,string_to_array($2,',')::bigint[])
  85. $function$;

差集, 全差集

需要用到不同入参时,仿照上边sql扩展即可

create or replace function array_diffset(anyarray, anyarray)
  returns anyarray
  immutable
  language sql
as $function$
    select array(
        select unnest($1)
        except
        select unnest($2)
    );
$function$;

create or replace function array_diffset_full(anyarray, anyarray)
  returns anyarray
  immutable
  language sql
as $function$
    with t1(n) as (
        select unnest($1)
        except
        select unnest($2)
    ),t2(n) as (
        select unnest($2)
        except
        select unnest($1)
    ) select array(
        select n from t1
          union
        select n from t2
    );
$function$;