sql and or priority issues

Just encountered such a problem in the project, the SQL statement is as follows:

select * from LOAN_BACK_LIBRARY where LIBRARY_ID=1 or LIB_ID=1 and STATUS=3

The condition of the result I want is: 1. LIBRARY_ID=1 or LIB_ID=1

                                       2. STATUS=3

but the result is not so, STATUS appeared! =3, but it matches LIBRARY_ID=1 or LIB_ID=1

. Why?

The original SQL execution is like this:

select * from LOAN_BACK_LIBRARY where LIBRARY_ID=1 or LIB_ID=1 and STATUS=3

is modified to:

select * from LOAN_BACK_LIBRARY where STATUS=3 and LIBRARY_ID=1 or LIB_ID=1 is still incorrect

Oh , I found the problem:

If there is an and, or condition after the where, or will automatically separate the left and right query conditions, that is, execute the and first, and then execute the or . The reason is: and has the highest execution priority!

The high to low priority of relational operators is: not and or

The solution to the problem is:

use () to change the execution order! ! ! !

The SQL statement I need above is like this

select * from LOAN_BACK_LIBRARY where STATUS=3 and (LIBRARY_ID=1 or LIB_ID=1 )

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326568984&siteId=291194637