PostgreSQL检查数组是否包含左侧数组中的任何元素
问题描述:
我知道在PostgreSQL中您可以运行以下查询:
I know that in PostgreSQL you can run a query like:
SELECT (1 = ANY('{1,3,4,7}'::int[])) AS result
检查右侧数组是否包含元素1
.我想知道是否有一种简单的方法来检查右侧数组是否包含 left 数组中的任何元素.像这样:
SELECT (1 = ANY('{1,3,4,7}'::int[])) AS result
to check if the right-hand array contains the element 1
. I was wondering if there is an easy way to check if the right-hand array contains any element from the left-hand array. Something like:
SELECT ('{2,3}'::int[] = ANY('{1,3,4,7}'::int[])) AS result
是否有一种简单的方法可以执行此操作,而无需自己遍历左侧循环?
Is there an easy way to do this without iterating over the left-hand loop myself?
答
当然,请使用&&
array-overlaps运算符:
Sure, use the &&
array-overlaps operator:
SELECT ARRAY[1,2] && ARRAY[1,3,4,7];
请参见数组函数和运算符.