Multiple conditions in query

Hi All!

I was wondering if it’s possible to set multiple conditions in a query like :

SELECT * FROM contacts WHERE asker_id = '$user_ID' OR receiver_id = '$user_ID' AND status = 1

My goal is to find out how many CONFIRMED (status = 1) connections a user with the given ID has…

Hope someone can help…

Thank you guys! That’s exactly what I needed!

By the way, I agree with r937…

Have a nice day!

i hate that default mysql colour coding

those lime green parentheses are practically invisible, and they’re the most important part of the statement

Yes, it’s possible. But once you’re using AND and OR in one query, you’ll have to tell MySQL the order of things (using parentheses):

FROM contacts 
WHERE (asker_id = '$user_ID' OR receiver_id = '$user_ID')
AND status = 1

If you don’t use ( and ), then the outcome might not be what you expect :slight_smile:

yes, that’s possible

although you will want to use parentheses when you are mixing ANDs and ORs

these two are fundamentally different –

WHERE ( asker_id = ‘$user_ID’ OR receiver_id = ‘$user_ID’ ) AND status = 1

WHERE asker_id = ‘$user_ID’ OR ( receiver_id = ‘$user_ID’ AND status = 1 )

if you leave out the parentheses, it’s the same as the second one