Oracle的where語句和排序操作

where特點:

1、用於對數據的篩選

2、可以比較,邏輯操作

3、where 需要放到from後面


=====================================================


一、比較操作

比較操作包含:>   <    >=    <=    in   (not in)    between ... and ...     like 等


1、使用數字做條件

     SQL>select ename,sal,deptno from emp where DEPTNO=10;

     SQL>select * from emp where sal>1000;


2、使用字符做條件

     SQL>select ename,sal,deptno from emp where ename='SCOTT';

     注意:where後面的字符需要使用單引號引起來,並且where後的字符嚴格區分大小寫


3、between ... and ... :限制數據在某個範圍

     SQL>select * from emp where sal between 1000 and 3000;

     注意:between是包含關係。


4、in:使用枚舉的形式查詢數據

     SQL>select * from emp where ename in ('KING','SCOTT','ALLEN');


5、like:用於模糊匹配

     %  :表示0個或者多個字符

      _  :表示一個字符


     ①找到僱員名字以M開頭的emp信息

     SQL>select * from emp where ename like '%M';


     ②找到字符串中包含M的僱員信息

     SQL>select * from emp where ename like '%M%';


     ③找到名字第二個字母爲M的僱員信息

     SQL>select * from emp where ename like '_M%';


6、注意: 可以使用escape轉義%或_

     SQL> select * from t11 where name like '%_%';

       NAME

       ---------

       aa_a

       aaa

     SQL> select * from t11 where name like '%\_%' escape '\';

       NAME

       ----------

       aa_a


7、對null的處理

     SQL>select * from emp where comm is null;

     SQL>select * from emp where comm is not null;


=====================================================


二、邏輯操作

1、and   要求所有表達式爲true,才能爲true

2、or      所有表達式中只要有一個爲true就返回true

3、not    取反


①查詢部門編號爲10,並且工資大於1500的人

SQL>select * from emp where sal>1500 and deptno=10;


②查詢部門編號爲10或者工資大於1500的人

SQL>select * from emp where sal>1500 or deptno=10;


③使用not,not表示取反

SQL>select * from emp where ename not in ('KING','SCOTT','ALLEN');


=====================================================


三、where中條件的優先級

1、算術操作

2、比較操作

3、邏輯操作:not>and>or


①找到工作爲管理員或者是分析員,並且工資大於2500的人

SQL>select * from emp where (job='MANAGER' or job='ANALYST') and sal >2500;

     EMPNO    ENAME         JOB          MGR       HIREDATE      SAL     COMM  DEPTNO

    ----------  ----------  ------------ --------- ------------- -------- --------- ----------

      7566        JONES      MANAGER    7839     02-APR-81     2975                      20

      7698        BLAKE      MANAGER    7839     01-MAY-81    2850                      30

      7782        CLARK      MANAGER    7839     09-JUN-81     2450                      10

      7788        SCOTT      ANALYST      7566     19-APR-87     3000                      20

      7902        FORD       ANALYST       7566     03-DEC-81    3000                      20

      

=====================================================


四、排序

1、ASC   升序排列(默認)

2、DESC 降序排列

     SQL>select ename,sal A from emp where comm is null  order by A;   默認是升序

     SQL>select ename,sal A from emp where comm is null  order by A desc;


3、order by :可以使用數字

     SQL>select * from emp order by 6;


4、多列排序

     按照deptno 做降序排列,sal做升序排列。

     SQL>select ename,deptno,sal from emp order by deptno desc,sal ;

     SQL>select ename,deptno,sal from emp order by 2,3 desc;



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章