snippetsqlMinor
How to check multidimensional array of arrays contains array?
Viewed 0 times
arraysmultidimensionalarraycontainshowcheck
Problem
I have an array of arrays [ID, TYPE].
All values are integer. Array can be
How can I check this array(s) contains array [443,2]?
EDIT
One of the way to check described here.
For my case, I want get False in
All values are integer. Array can be
integer[] or jsonb type.SELECT array[[442,2],[443,2]]
SELECT to_jsonb(array[[1,2],[3,4]])How can I check this array(s) contains array [443,2]?
EDIT
One of the way to check described here.
1# SELECT array[[442,2],[443,2]] @> array[443,2] -- returns True
2# SELECT array[[442,2],[443,2]] @> array[2,443] -- returns True tooFor my case, I want get False in
2#Solution
Technically there is no such thing as a two-dimensional array in Postgres. Quote from the manual:
The current implementation does not enforce the declared number of dimensions either. [...] So, declaring the array size or number of dimensions in CREATE TABLE is simply documentation; it does not affect run-time behavior
So the value
You can however do that if you declare an array of a row type.
Then you can do the following:
but
The current implementation does not enforce the declared number of dimensions either. [...] So, declaring the array size or number of dimensions in CREATE TABLE is simply documentation; it does not affect run-time behavior
So the value
array[[442,2],[443,2]] is internally stored as array[442,2,443,2] and that's the reason why array[[442,2],[443,2]] @> array[2,443] is true, because it is internally represented as array[442,2,443,2] @> array[2,443] which obviously is true.You can however do that if you declare an array of a row type.
create type idtype as (id integer, type integer);Then you can do the following:
select array[(442,2), (443,2)]::idtype[] @> array[(442,2)]::idtype[];
--> truebut
select array[(442,2), (443,2)]::idtype[] @> array[(2,442)]::idtype[];
--> falseCode Snippets
create type idtype as (id integer, type integer);select array[(442,2), (443,2)]::idtype[] @> array[(442,2)]::idtype[];
--> trueselect array[(442,2), (443,2)]::idtype[] @> array[(2,442)]::idtype[];
--> falseContext
StackExchange Database Administrators Q#192840, answer score: 7
Revisions (0)
No revisions yet.