这句SQL语句没有明显的语法错误,但是逻辑可能存在问题。因为'and'运算符的优先级高於'or'运算符,所以这个SQL语句的实际逻辑是:

select distinct dept  
from xx_equipment_info 
where (org_code='CH' and  enabled='Y' and form_ver is null) 
or desc_1 is null 
order by dept,code

如果这正是您想要的逻辑,那么这句SQL是沒有问题的。但是如果您的意图是先匹配org_code、enabled和form_ver,然後再检查desc_1,那么您需要使用括号来明确优先级:

select distinct dept  
from xx_equipment_info 
where org_code='CH' and  enabled='Y' and (form_ver is null or desc_1 is null) 
order by dept,code

这样的话,这句SQL的逻辑就是先匹配org_code、enabled和form_ver,然后检查desc_1。

Oracle SQL语句逻辑问题分析:如何正确使用括号提升查询效率

原文地址: https://www.cveoy.top/t/topic/oaAE 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录