1

Previously i try to use overlap to find data where data has any of values in array. I use postgres

SQL statement was:

SELECT *,
ARRAY_AGG("reserve"."state_id" ) AS "states" 
FROM "orders_order"
JOIN "reserve" ON ("order"."id" = "reserve"."order_id")
GROUP BY "order"."id"
HAVING (ARRAY_AGG("reserve"."state_id" )&& array['test', 'test_new']::varchar(255)[])

For example if my states where like below:

{'test', 'new_test' }

It gives me all reserves if states test was in array

Now i need receive states if only ['test'] in array

Expected output must be empty in above mentioned example

Some examples to clarify:

1. {'test', 'new_test' } -> false
2. {'test', 'test' } -> true
3. {'test'} -> true

How can i achieve this. What needs to add to my SQL statement?

1
  • 1
    SQL does not support arrays in general. Tag with the database you areusing. Commented Jul 25, 2021 at 19:00

1 Answer 1

1

You're looking for @> (contains operator):

SELECT 
 array['test'] @> array['test','new_test'], --false
 array['test'] @> array['test','test'],     --true
 array['test'] @> array['test'];            --true

 ?column? | ?column? | ?column? 
----------+----------+----------
 f        | t        | t
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.