如何比较两个数组并仅选择Postgres中的不匹配元素 [英] How to compare two arrays and pick only the non matching elements In postgres

查看:57
本文介绍了如何比较两个数组并仅选择Postgres中的不匹配元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何只选择两个数组之间不匹配的元素。



示例:

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

所以在这里我想比较两个数组并从基本数组中删除匹配的元素。



现在base_array应该像 [12,5]

解决方案

我会使用数组来解决这个问题

 选择数组(选择unnest(:arr1),但选择unnest(:arr2)); 

如果:arr1和:arr2不相交,则使用array_agg()会导致null。 / p>

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 want to compare both the arrays and remove the matching elements from the base array.

Now base_array should be like [12,5]

解决方案

I'd approach this with the array operator.

select array(select unnest(:arr1) except select unnest(:arr2));

If :arr1 and :arr2 don't intersect, using array_agg() leads to a null.

这篇关于如何比较两个数组并仅选择Postgres中的不匹配元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆