Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Select all rows that fully intersect with the input array?

Consider the following table

id int lang_codes text[]
1 {eng,fre}
2 {eng}

I need to select all the rows from the table that intersect with the input array, but only when all the items in the lang_codes are present in the input array, e.g.:

  • if the input array contains [eng,fre,ger] then it returns all the rows
  • while [eng,ger] won’t return the first record, because it needs both codes to be present in the input array.

I’ve tried the && operand, but it returns all the rows:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

select * from my_table where lang_codes && ARRAY['eng','ger'];

From the other hand @> operand, returns only when array matches fully

>Solution :

The <@ operator should do the trick:

select * from my_table where lang_codes <@ ARRAY['eng','ger'];

SQL Fiddle

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading