Detailed use of or in mysql (mixed use of and and or in Mysql) MES

Detailed use of or in mysql (mixed use of and and or in Mysql)
In mysql, such a situation is often encountered. When writing a conditional statement where, there may be "or" or "and" of multiple conditions at the same time. But it often fails to achieve the effect. After Baidu, I found that when the condition "and" or "or" appears in a where statement at the same time, it is necessary to enclose multiple ORs in parentheses and then perform "and" with AND, or Enclose multiple ANDs in parentheses and perform "or" with OR.

eg. select * from table from id=1 or id=2 and price>=10;

This statement is executed by default if id=2 and price is greater than or equal to 10, or id=1.

If you add brackets: select * from table from (id=1 or id=2) and price>=10;

Then this statement executes id=1 or id=2, and the price is greater than or equal to 10.

Mixed use of or and and. First of all, it should be clear that and has a higher priority than or.

Summary: To use the mixed use of and and or, the or condition must be included together with () to form a part, otherwise, the expected effect cannot be achieved.

Guess you like

Origin blog.csdn.net/liufeifeihuawei/article/details/129829279