"how to use one or more or and and in mysql query " Code Answer

2

there is precedence with logical operators. when in doubt, use parenthesis.

in your case:

select * from database where (name = var1 or name = var2 or name = var3) and id < 200

your original query was interpreted as follows because the and has higher precedence.

select * from database where name = var1 or name = var2 or (name = var3 and id < 200)

update

as commented by rocket, you could condense your or statements to in since they operate on the same field. doing so would remove the need for parentheses.

select * from database where name in (var1,var2,var3) and id < 200

nonetheless, understanding the difference between the two original queries is important as you inevitably will write queries with multiple conditions.

By krishnaji on September 9 2022

Answers related to “how to use one or more or and and in mysql query ”

Only authorized users can answer the Search term. Please sign in first, or register a free account.