How to compare two arrays and pick only the non matching elements In postgres

后端 未结 7 1990
一整个雨季
一整个雨季 2020-12-14 06:15

How can I pick only the non matching elements between two arrays.

Example:

base_array [12,3,5,7,8]
temp_array [3,7,8]

So here I wan

7条回答
  •  忘掉有多难
    2020-12-14 06:54

    I've constructed a set of functions to deal specifically with these types of issues: https://github.com/JDBurnZ/anyarray

    The greatest thing is these functions work across ALL data-types, not JUST integers, as intarray is limited to.

    After loading loading the functions defined in those SQL files from GitHub, all you'd need to do is:

    SELECT
      ANYARRAY_DIFF(
        ARRAY[12, 3, 5, 7, 8],
        ARRAY[3, 7, 8]
      )
    

    Returns something similar to: ARRAY[12, 5]

    If you also need to return the values sorted:

    SELECT
      ANYARRAY_SORT(
        ANYARRAY_DIFF(
          ARRAY[12, 3, 5, 7, 8],
          ARRAY[3, 7, 8]
        )
      )
    

    Returns exactly: ARRAY[5, 12]

提交回复
热议问题