请教大神,为什么方法一中还能出现950的sal,前面不是已经有“sal>1000”的条件了吗,能不能帮我分析一下这两种方法的区别,感激!方法一:
SQL> select ename ,sal ,deptno from emp where sal>1000 and deptno=20 or deptno=3
0;ENAME             SAL     DEPTNO
---------- ---------- ----------
ALLEN            1600         30
WARD             1250         30
JONES            2975         20
MARTIN           1250         30
BLAKE            2850         30
SCOTT            3000         20
TURNER           1500         30
ADAMS            1100         20
JAMES             950         30
FORD             3000         20已选择10行。方法二:
SQL> select ename ,sal,deptno from emp where sal>1000 and( deptno=20 or deptno=3
0);ENAME             SAL     DEPTNO
---------- ---------- ----------
ALLEN            1600         30
WARD             1250         30
JONES            2975         20
MARTIN           1250         30
BLAKE            2850         30
SCOTT            3000         20
TURNER           1500         30
ADAMS            1100         20
FORD             3000         20已选择9行。oracleSQLselect

解决方案 »

  1.   

    and 优先级高于 or
    出现这样的问题时,做好用 () 来约束,易于理解,又比较高的可读性
      

  2.   

    这是优先级的问题,and 的优先级是大于or的
      

  3.   

    select ename ,sal ,deptno from emp where sal>1000 and deptno=20 or deptno=3
    这个就相当于
    select ename ,sal ,deptno from emp where (sal>1000 and deptno=20) or (deptno=3)虽然950那条记录不满足(sal>1000 and deptno=20),但是它满足(deptno=3),所以也能查出来了、
      

  4.   

     sal>1000 and deptno=20 or deptno=30;
    相当于   (sal>1000 and deptno=20) or (deptno=30)
      

  5.   

    你的第一种情况从左往右依次执行的
    select ename ,sal ,deptno from emp where sal>1000 and deptno=20 or deptno=3
    这个就相当于
    select ename ,sal ,deptno from emp where (sal>1000 and deptno=20) or (deptno=3)虽然950那条记录不满足(sal>1000 and deptno=20),但是它满足(deptno=3),所以也能查出来了第二种情况sal>1000 and (deptno=20 or deptno=3) 先满足了扩好内的在去判断sal>1000的情况,由于是and的关系deptno=20 或者3的就被过滤掉了.
      

  6.   

    方法1相当于:select ename ,sal ,deptno from emp where (sal>1000 and deptno=20) or (deptno=3
     0);
      

  7.   

    1 and 优先级高于 or2 就算他们是平级,也是从左到右。
      

  8.   

    and和or是同级的顺序从左往右
      

  9.   

    select ename ,sal ,deptno from emp where sal>1000 and deptno=20 or deptno=3;
    select ename ,sal,deptno from emp where sal>1000 and( deptno=20 or deptno=3);
    出现这样的问题,是and和or优先级的问题,and优于or,所以第一个sql和
    select ename ,sal ,deptno from emp where (sal>1000 and deptno=20) or deptno=3;
    所以会出现了sal<1000的情况。 
      

  10.   

    and 优先级高于 or  受教了