0

The first one below works, but, just wanted to see if there's a better way...

If I'm trying to find all records that start with 'C' with either a flag of 2 or a status of 9, do I need to incorporate the 'C' criteria twice?

i.e.,

"SELECT * FROM mytable WHERE name like 'C%' AND flag = 2 OR name like 'C%' AND status = 9"

Or, is there a way quicker way to write it so that I only need to set 'C%' once?

2 Answers 2

1

Logically, AND and OR are distributive to each other, i.e.

a & (b | c) = (a & b) | (a & c)

that means your condition can be rewritten as

name LIKE 'C%' AND (flag = 2 OR status = 9)
Sign up to request clarification or add additional context in comments.

Comments

1

OR has lower priority than AND so you need parentheses.

WHERE name LIKE "C%" AND (flag = 2 OR flag = 9)

you can also check for membership of a set of values

... AND flag IN (1, 9)

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.