简体   繁体   English

SQL AND OR 运算符

[英]SQL AND OR operators

Why this below query ONLY brings deptno = 10 ?为什么下面的查询只带来deptno = 10
Why is not not bringing rows of job IN ('MANAGER','SALESMAN') and deptno = 30 results?为什么不将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) is always TRUE . (1=1)总是TRUE
So any condition/boolean expression combined with OR with that condition, like:因此,任何条件/布尔表达式与OR与该条件相结合,例如:

(job IN ('MANAGER','SALESMAN') and deptno = 30) or (1=1)

will also be always TRUE .也将始终为TRUE
This makes your WHERE clause equivalent to:这使您的WHERE子句等效于:

where deptno=10 and TRUE

and simpler:更简单:

where deptno=10

If you want the rows of the table of deptno=10 and the rows of deptno=30 only if job IN ('MANAGER','SALESMAN') then you should have:如果您只需要在job IN ('MANAGER','SALESMAN')时才需要deptno=10表的行和deptno=30的行,那么您应该具有:

where deptno=10 or ((job IN ('MANAGER','SALESMAN') and deptno = 30) 

You probably wanted to do this:你可能想这样做:

select * 
from emp 
where (deptno=10 )
  or ((job IN ('MANAGER','SALESMAN') and deptno = 30));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM