0

I have a ids integer[]

And, I want to find rows which contain 1 but must not contains 2, 3, 4

but [1] OR [1, 5] OR [1, 6, 7] <- this data is OK. [2,3,4] is not.

So I tried this way

SELECT * 
FROM table_test 
WHERE 1 = ANY(ids) AND 2 <> ANY(ids)  AND 3 <> ANY(ids)  AND 4 <> ANY(ids)

but it returns 1 = ANY(ids) part

[1 2 3]
[1 3 4]
[1]
[1 5]
[1 6 7]

I want this data

[1]
[1 5]
[1 6 7]

How can I solve this problem?

Thanks a lot!

2 Answers 2

1

You should use ALL together with <>.

The expression 2 <> ANY(ids) is true if at least one element is not equal to 2 - which is always the case because you require at least one element to be 1 (which is not 2) with the first condition.

SELECT * 
FROM table_test 
WHERE 1 = ANY(ids) 
  AND 2 <> ALL(ids)  
  AND 3 <> ALL(ids)  
  AND 4 <> ALL(ids)

another option is to use the overlaps operator && ("have elements in common") and negate it:

SELECT * 
FROM table_test 
WHERE 1 = ANY(ids) 
  AND NOT ids && array[3,4,5]
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! it worked. You gave me 2 choices. Is there any performance difference? Can you tell me which way is more efficient? In my case, the number which must be in is always one. but the numbers mst not be in are not fixed. it could be one or two or more.
Check the execution plan but I doubt there is a performance difference.
1

Your query is very close, but what is actually does is:

  1. check if any array element contains 1 (this is ok)
  2. check if any array element does not contain 2, 3 and 4 (this means [1,3,4] is valid beacuse 1 is not 2,3 or 4, so the condition is fulfilled)

What you really have to check with case #2 is that ALL elements are not 2, 3, 4.

Your updated query is now:

SELECT * FROM table_test WHERE 1 = ANY(ids) AND 2 <> ALL(ids)  AND 3 <> ALL(ids)  AND 4 <> ALL(ids);

1 Comment

Thanks for your kindness. your explanation is very useful!

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.