GENERAL - [How to check if the array contains the element.]

Started by VENKTESH GUTTEDARalmost 9 years ago3 messagesgeneral
Jump to latest
#1VENKTESH GUTTEDAR
venkteshguttedar@gmail.com

Hello All,

How do i check if any one element of one array exists in another array.
Eg:
array1 = [1,2,3,4]
array2 = [1,4,5,7]
Now i expect the result as true even if any one element matches from
array2 in array1.

Any help would be appreciated.

Thank you.

--
Regards :
Venktesh Guttedar.

#2Andreas Kretschmer
andreas@a-kretschmer.de
In reply to: VENKTESH GUTTEDAR (#1)
Re: GENERAL - [How to check if the array contains the element.]

Am 26.04.2017 um 13:24 schrieb VENKTESH GUTTEDAR:

Hello All,

How do i check if any one element of one array exists in another
array.
Eg:
array1 = [1,2,3,4]
array2 = [1,4,5,7]
Now i expect the result as true even if any one element matches
from array2 in array1.

You can use the overlap - operator:

test=*# select array[1,2,3,4] && array[1,4,5,7];
?column?
----------
t
(1 Zeile)

test=*# select array[1,2,3,4] && array[10,40,50,70];
?column?
----------
f
(1 Zeile)

Regards, Andreas

--
2ndQuadrant - The PostgreSQL Support Company.
www.2ndQuadrant.com

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

#3Pavel Stehule
pavel.stehule@gmail.com
In reply to: Andreas Kretschmer (#2)
Re: GENERAL - [How to check if the array contains the element.]

Hi

2017-04-26 13:33 GMT+02:00 Andreas Kretschmer <andreas@a-kretschmer.de>:

Am 26.04.2017 um 13:24 schrieb VENKTESH GUTTEDAR:

Hello All,

How do i check if any one element of one array exists in another
array.
Eg:
array1 = [1,2,3,4]
array2 = [1,4,5,7]
Now i expect the result as true even if any one element matches from
array2 in array1.

You can use the overlap - operator:

test=*# select array[1,2,3,4] && array[1,4,5,7];
?column?
----------
t
(1 Zeile)

test=*# select array[1,2,3,4] && array[10,40,50,70];
?column?
----------
f
(1 Zeile)

or subset

postgres=# select array[1,2,3,4] @> array[1,3,2,4];
┌──────────┐
│ ?column? │
╞══════════╡
│ t │
└──────────┘
(1 row)

Show quoted text

Regards, Andreas

--
2ndQuadrant - The PostgreSQL Support Company.
www.2ndQuadrant.com

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general