优秀的编程知识分享平台

网站首页 > 技术文章 正文

SQL之数据限定与排序(sql限制数据位数)

nanyue 2024-07-20 23:52:12 技术文章 10 ℃

【六】数据限定与排序

6.1 SQL语句的编写规则

1)SQL语句是不区分大小写的,关键字通常使用大写;其它文字都是使用小写

2)SQL语句可以是一行,也可以是多行,但关键字不能在两行之间一分为二或缩写

3)子句通常放在单独的行中,这样可以增强可读性并且易于编辑

4)使用缩进是为了增强可读性

简单查询语句执行顺序:一般是指一个SELECT查询结构,仅访问一个表。

基本语法如下:

SELECT 子句— 指定查询结果集的列组成,列表中的列可以来自一个或多个表或视图。

FROM 子句— 指定要查询的一个或多个表或视图。

WHERE 子句— 指定查询的条件。

GROUP BY 子句— 对查询结果进行分组的条件。

HAVING 子句— 指定分组或集合的查询条件。

ORDER BY 子句— 指定查询结果集的排列顺序

语句执行的一般顺序为①from, ②where, ③group by, ④having, ⑤select, ⑥order by

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)>7000;

也存在一些不规范的写法:不建议采用。如上句改成having位置 在group by之前

SQL> select deptno,sum(sal) from emp having sum(sal)>7000 group by deptno;

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;

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;

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

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

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;

8SQL> 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);

1)nvl和nvl2中的第二个参数不是一回事。

2)nvl2的第二个参数和第三个参数要一致,如果不一致,第三个参数隐形转换成第二个参数

2)NULLIF(expr1,expr2)

当第一个参数和第二个参数相同时,返回为空,当第一个参数和第二个数不同时,返回第一个参数值,第一个参数值不允许为null

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

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

3)coalesce(expr1,expr2........)

返回从左起始第一个不为空的值,如果所有参数都为空,那么返回空值。

这里所有的表达式都是同样的数据类型:

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

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


the end !!!

@jackman 共筑美好!

Tags:

最近发表
标签列表