2

I'm not sure why this structure is not working. This is my first time working with postgres and was hoping someone could help me.

SELECT * FROM "friends" WHERE "from" = '1' OR "to" = '1' AND "status" = '1'

It returns all values where from where "from" is = 1 and "to" = 1 rather than one or the other where "status" is = 1

I hope that isn't too confusing.

Thanks.

0

1 Answer 1

3

OR operator has lower precedence than AND [1]. As a result, the expression is evaluated as follows:

(
    "from" = '1'
)
OR
(
    "to" = '1'
    AND
    "status" = '1'
)

What you probably want instead is:

SELECT * FROM "friends" WHERE ("from" = '1' OR "to" = '1') AND "status" = '1'
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.