oracle之数据限定与排序

摘要:
SQL˃selectedame,salfromtemprderbysal;SQL˃selectedname,salasalaryfrommporderbasalary;SQL˃selectedame,salasalaryfrommporderb2;SQL˃从emporderbysal+comm中选择name、sal、sal+100;SQL˃selectdeptno,avgfromempgroupbydeptnoorderbyavgdesc;SQL˃selectedname、job、sal+commfromemprderby3nullsfirst;SQL˃selectedname、deptno、jobfrompordbyptnoasc、jobdesc;6.3 Null值Null值既不是值0,也不是字符“”。Null表示不确定性。2) 分组函数忽略空值SQL˃selectsum,sumfromp//为什么sal+comm的和小于sal的和?SQL˃selectename,sal,commfrompwhere sal˃=comm;ENAMEALCOMM------------------------------ALLEN1600300WARD1250500TURNER1504)当“||”用于非空字段和空值字段时,空值将转换为字符类型“”,合并列的数据类型为varchar2.SQL˃selectnvl(1,2)fromdual;NVL(1,2)---------1SQL˃从双通道选择NVL;NVL-------------2*测试点:NVL函数可用于数字类型、字符类型和日期类型,但数据类型应尽可能匹配。

数据限定与排序

6.1 简单查询语句执行顺序

from, where, group by, having, order by, select

where限定from后面的表或视图,限定的选项只能是表的列或列单行函数或列表达式,where后不可以直接使用分组函数

SQL> select empno,job from emp where sal>2000;
SQL> select empno,job from emp where length(job)>5;
SQL> select empno,job from emp where sal+comm>2000;

having限定group by的结果,限定的选项必须是group by后的聚合函数或分组列,不可以直接使用where后的限定选项。

SQL> select sum(sal) from emp group by deptno having deptno=10;
SQL> select deptno,sum(sal) from emp group by deptno having sum(sal)>9000;

如果要使用group by及having,有条件的话先使用where筛选。

6.2 排序(order by)

1)位置:order by语句总是在一个select语句的最后面。
2)排序可以使用列名,列表达式,列函数,列别名,列位置编号等都没有限制,select的投影列可不包括排序列,除指定的列位置标号外。
3)升序和降序,升序ASC(默认), 降序DESC。有空值的列的排序,缺省(ASC升序)时 null排在最后面(考点)。
4)混合排序,使用多个列进行排序,多列使用逗号隔开,可以分别在各列后面加升降序。

SQL> select ename,sal from emp order by sal;
SQL> select ename,sal as salary from emp order by salary;
SQL> select ename,sal as salary from emp order by 2;
SQL> select ename,sal,sal+100 from emp order by sal+comm;
SQL> select deptno,avg(sal) from emp group by deptno order by avg(sal) desc;
SQL> select ename,job,sal+comm from emp order by 3  nulls first;
SQL> select ename,deptno,job from emp order by deptno asc,job desc;

6.3 空值(null)

空值既不是数值0,也不是字符" ", null表示不确定。

6.3.1 空值参与运算或比较时要注意几点:

1)空值(null)的数据行将对算数表达式返回空值

SQL> select ename,sal,comm,sal+comm from emp;

ENAME             SAL       COMM   SAL+COMM
---------- ---------- ---------- ----------
SMITH             800
ALLEN            1600        300        1900
WARD             1250       500        1750
JONES            2975
MARTIN         1250       1400       2650
BLAKE            2850
CLARK            2450
SCOTT            3000
KING              5000
TURNER         1500            0       1500
ADAMS          1100
JAMES            950
FORD             3000
MILLER           1300

已选择14行。

2)分组函数忽略空值

SQL> select sum(sal),sum(sal+comm) from emp;        //为什么sal+comm的求和小于sal的求和?

  SUM(SAL) SUM(SAL+COMM)
---------- -------------
     29025          7800


3)比较表达式选择有空值(null)的数据行时,表达式返回为“假”, 结果返回空行。

SQL>  select ename,sal,comm from emp where sal>=comm;

ENAME             SAL       COMM
---------- ---------- ----------
ALLEN             1600        300
WARD             1250        500
TURNER          1500          0


4)非空字段与空值字段做"||"时, null值转字符型"",合并列的数据类型为varchar2。


SQL> select ename, sal||comm from emp;

ENAME          SAL||COMM
---------- --------------------------------------------------------------------------------
SMITH          800
ALLEN          1600300
WARD           1250500
JONES          2975
MARTIN         12501400
BLAKE          2850
CLARK          2450
SCOTT          3000
KING               5000
TURNER         15000
ADAMS          1100
JAMES          950
FORD           3000
MILLER         1300

已选择14行。

5)not in 在子查询中的空值问题(见第八章)

6)外键值可以为null,唯一约束中,null值可以不唯一(见十二章)

7)空值在where子句里使用“is null”或“is not null”

SQL> select ename,mgr from emp where mgr is null;

SQL> select ename,mgr from emp where mgr is not null;

8)空值在update语句和insert语句可以直接使用“=null” 赋值

SQL> update emp set comm=null where empno=7788;


6.3.2 处理空值的几种函数方法:

1)nvl(expr1,expr2)
当第一个参数不为空时取第一个值,当第一个值为NULL时,取第二个参数的值。

SQL>select nvl(1,2) from dual;

  NVL(1,2)
----------
         1

SQL> select nvl(null,2) from dual;

NVL(NULL,2)
-----------
         2

*考点:nvl函数可以作用于数值类型,字符类型,日期类型,但数据类型尽量匹配。

NVL(comm,0)
NVL(hiredate,'1970-01-01')
NVL(ename,'no manager')

2)nvl2(expr1,expr2,expr3)

当第一个参数不为NULL,取第二个参数的值,当第一个参数为NULL,取第三个数的值。

SQL> select nvl2(1,2,3) from dual;

NVL2(1,2,3)
-----------
         2

SQL> select nvl2(null,2,3) from dual;

NVL2(NULL,2,3)
--------------
            3

SQL> select ename,sal,comm,nvl2(comm,SAL+COMM,SAL) income,deptno from emp where deptno in (10,30);

ENAME                 SAL       COMM     INCOME     DEPTNO
---------- ---------- ---------- ---------- ----------
ALLEN                1600        300       1900         30
WARD                 1250        500       1750         30
MARTIN               1250       1400      2650         30
BLAKE                2850                    2850         30
CLARK                2450                     2450         10
KING                 5000                     5000         10
TURNER               1500          0         1500         30
JAMES                 950                       950         30
MILLER               1300                     1300         10


*考点: nvl和nvl2中的第二个参数不是一回事。

3)NULLIF(expr1,expr2)  /*比对两个值是否一样,一样就返回为空,否则不会为空*/
当第一个参数和第二个参数相同时,返回为空,当第一个参数和第二个数不同时,返回第一个参数值,第一个参数值不允许为null

SQL> select nullif(2,2) from dual;

NULLIF(2,2)
-----------

SQL> select nullif(1,2) from dual;

NULLIF(1,2)
-----------
          1

4)coalesce(expr1,expr2........) 返回从左起始第一个不为空的值,如果所有参数都为空,那么返回空值。

SQL> select coalesce(1,2,3,4) from dual;


COALESCE(1,2,3,4)
-----------------
                1

SQL> select coalesce(null,2,null,4) from dual;


COALESCE(NULL,2,3,4)
--------------------
                  2

免责声明:文章转载自《oracle之数据限定与排序》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇System.IO 应用一.netcore在linux下使用P/invoke方式调用linux动态库下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

MySQL高级知识(二)——Join查询

前言:该篇主要对MySQL中join语句的七种情况进行总结。 0.准备 join主要根据两表或多表之间列的关系,从这些表中进行数据的查询。 首先创建两张表:tb_emp(员工表)和tb_dept(部门表),并插入相关测试数据。 1.tb_emp表。 DROP TABLE IF EXISTS `tb_emp`; CREATE TABLE `tb_emp`...

select默认下拉箭头改变、option样式清除

谷歌、火狐、ie下 select 的默认下拉箭头图标差别还是比较大,一般我们都会清除默认样式,重新设计箭头图标; /* --ie清除--*/ select::-ms-expand{ display: none; } /* --火狐、谷歌清除--*/ select{ appearance:none; -moz-appearan...

sql server中部分函数功能详解

1.TOP 子句 TOP 子句用于规定要返回的记录的数目。 对于拥有数千条记录的大型表来说,TOP 子句是非常有用的。 SQL Server 的语法: SELECT TOP number|percent column_name(s) FROM table_name 2.’%%’查询 我们希望从上面的 "Persons" 表中选取居住的城市以 "A" 或...

最高优先级

最高优先级 #include<stdio.h> #include<stdlib.h> #include<string.h> #define Max 100 typedef struct pcb {     char name[Max];  //进程名     int priority;    //优先级     int...

C# ManagedDataAccess 操作oracle数据库

C# ManagedDataAccess 操作oracle数据库 甲骨文官方制作的oracle类库优点是无需安装oracle客户端 环境准备 Nuget安装 Oracle.ManagedDataAccess 引用 using Oracle.ManagedDataAccess.Client; 连接oracle数据库 连接字符串 connStr = @"Da...

MyBatis_2

目录 使用接口结合xml文件 数据库表与实体类创建 Users的操作类 Users映射文件 测试 解决字段与属性不匹配(以Order表为例) 数据库表与实体类创建 通过给查询字段起别名,要求别名与属性名一致 使用resultMap标签来定义实体类与字段之间的关系 实现链表查询【根据班级ID查询班级信息(带有老师信息)】 数据库表与实体类...