欢迎光临
我们一直在努力

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;

赞(0)
【声明】:本博客不参与任何交易,也非中介,仅记录个人感兴趣的主机测评结果和优惠活动,内容均不作直接、间接、法定、约定的保证。访问本博客请务必遵守有关互联网的相关法律、规定与规则。一旦您访问本博客,即表示您已经知晓并接受了此声明通告。