温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Oracle的where语句和排序操作

发布时间:2020-06-14 14:22:38 阅读:593 作者:大Q小Q 栏目:关系型数据库
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

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;

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI

开发者交流群×