Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres array lookup multiple values in where clause

I have below table with data

create table stud(key int, person text, subject_id int[]);

insert into stud select 1,'Alex',array[2,7,9];
insert into stud select 2,'Peter',array[4,9,12];
insert into stud select 3,'Tokaver',array[8];
insert into stud select 4,'Machel',array[11,15];

Table looks

enter image description here

I can filter single subject_id in where like

select * from stud where 9=any(subject_id)

How can we filter more than one subject_id in where clause like

select * from stud where (8,9) in any(subject_id)
like image 683
Hari Avatar asked Apr 09 '26 00:04

Hari


1 Answers

demo:db<>fiddle

You can use the overlap operator && for arrays:

select * from stud 
where array[8,9] && subject_id
like image 135
S-Man Avatar answered Apr 10 '26 17:04

S-Man



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!