为什么下面的查询只带来deptno = 10
为什么不带来job IN ('MANAGER','SALESMAN') and deptno = 30
结果的行?
select *
from emp
where deptno=10
and ((job IN ('MANAGER','SALESMAN') and deptno = 30) or (1=1));
(1=1)
始终为TRUE
因此,任何与OR
组合的条件/布尔表达式都具有该条件,如:
(job IN ('MANAGER','SALESMAN') and deptno = 30) or (1=1)
也将始终是CCD_ 6
这使您的WHERE
子句等效于:
where deptno=10 and TRUE
更简单:
where deptno=10
如果你想要deptno=10
的表中的行,而deptno=30
的行仅当job IN ('MANAGER','SALESMAN')
时,那么你应该有:
where deptno=10 or ((job IN ('MANAGER','SALESMAN') and deptno = 30)
您可能想要这样做:
select *
from emp
where (deptno=10 )
or ((job IN ('MANAGER','SALESMAN') and deptno = 30));