sql语句中的where条件执行顺序

语句1:

select *

from
WORK_ORDER_LIST

WHERE

head_corp_id = 1

and work_order_state in (1,2)

and receiving_id is null or receiving_id = 27

语句2:

select *
from
WORK_ORDER_LIST
WHERE head_corp_id = 1
and receiving_id is null or receiving_id = 27 

and work_order_state in (1,2)


在执行sql语句时,数据库中通过receiving_id is null or receiving_id = 27可获取3条数据,work_order_state in (1,2)可获取2条数据,

当我使用语句1获取到了两条数据,使用语句2获取到了三条数据。

理由是:sql解析器采用自下而上的顺序解析WHERE子句,通过此原理可以通过将查询条件范围小的语句靠近where,来实现优化。



你可能感兴趣的:(java)