I have a table mydata jsonb column containing an array of integers.
create table mydata
(
name varchar,
data jsonb
);
Here's some test data:
insert into mydata (name, data)
VALUES
('hello1', '[1]'),
('hello12', '[1,2]'),
('hello2', '[2]'),
('hello23', '[2,3]')
;
I now would like to query the table for elements, that contain either 2 or 3 (or both) in "data". Is there a better syntax for this besides:
select * from mydata where (data @> '2' or data @> '3');
because it could be that I have more than 2 options to query for of course. I would assume that I'm able to do a subquery like this (not working, just as hint what I'd like to achieve):
create table other ( id bigserial , text varchar);
insert into other (id, text) values (1, 'x'), (2, 'y'), (3, 'y'), (4, 'z');
What I now want to do is, get all data from mydata where data has a reference to other_table
select * from mydata where (data @> IN (select distinct id from other_table where text='y'));
Thanks a lot, Fritz
&&- but JSONB doesn't support that.int[]instead ofjsonbthen this type of queries is way easier (where data && array[2,3])