02 过滤和排序数据 -oracle学习笔记
select ....
from table1
where ....
order by ....
//where
1 有关日期格式属性修改 常识
NLS_DATE_FORMAT DD-MON-RR
select sysdate from dual;
NLS_CURRENCY ¥
SQL> select * from v$nls_parameters;
PARAMETER VALUE
---------------------------------------------------------------- -------------------------------------------
---------
NLS_LANGUAGE SIMPLIFIED CHINESE
NLS_TERRITORY CHINA
NLS_CURRENCY ¥
NLS_ISO_CURRENCY CHINA
NLS_NUMERIC_CHARACTERS .,
NLS_CALENDAR GREGORIAN
NLS_DATE_FORMAT DD-MON-RR
NLS_DATE_LANGUAGE SIMPLIFIED CHINESE
NLS_CHARACTERSET ZHS16GBK
NLS_SORT BINARY
NLS_TIME_FORMAT HH.MI.SSXFF AM
NLS_TIMESTAMP_FORMAT DD-MON-RR HH.MI.SSXFF AM
NLS_TIME_TZ_FORMAT HH.MI.SSXFF AM TZR
NLS_TIMESTAMP_TZ_FORMAT DD-MON-RR HH.MI.SSXFF AM TZR
NLS_DUAL_CURRENCY ¥
NLS_NCHAR_CHARACTERSET AL16UTF16
NLS_COMP BINARY
NLS_LENGTH_SEMANTICS BYTE
NLS_NCHAR_CONV_EXCP FALSE
已选择19行。
alter session set NLS_DATE_FORMAT='DD-MON-RR';
alter session set NLS_DATE_FORMAT='yyyy-mm-dd';
----查询比81年1月1号 入职晚的员工
1 select *
2 from emp
3* where hiredate > '01-1月 -81'
SQL> /
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- -------------- ---------- ---------- ----------
7499 ALLEN SALESMAN 7698 20-2月 -81 1600 300 30
7521 WARD SALESMAN 7698 22-2月 -81 1250 500 30
7566 JONES MANAGER 7839 02-4月 -81 2975 20
7654 MARTIN SALESMAN 7698 28-9月 -81 1250 1400 30
7698 BLAKE MANAGER 7839 01-5月 -81 2850 30
7782 CLARK MANAGER 7839 09-6月 -81 2450 10
7788 SCOTT ANALYST 7566 19-4月 -87 3000 20
7839 KING PRESIDENT 17-11月-81 5000 10
7844 TURNER SALESMAN 7698 08-9月 -81 1500 0 30
7876 ADAMS CLERK 7788 23-5月 -87 1100 20
7900 JAMES CLERK 7698 03-12月-81 950 30
7902 FORD ANALYST 7566 03-12月-81 3000 20
7934 MILLER CLERK 7782 23-1月 -82 1300 10
已选择13行。
------- oracle支持 隐式类型转换 ..eg char ****====>date,抛砖
-------- 日期转换的函数.....
------ 日期和字符串''
-------日期是格式敏感
select ....
from .....
where col > 30
col in ()
col between a and b .. a要小b []
1 查询工资在1000~2000之间的员工信息
select *
from emp
where sal between 1000 and 2000
select *
from emp
where sal >=1000 and sal <=2000
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- -------------- ---------- ---------- ----------
7499 ALLEN SALESMAN 7698 20-2月 -81 1600 300 30
7521 WARD SALESMAN 7698 22-2月 -81 1250 500 30
7654 MARTIN SALESMAN 7698 28-9月 -81 1250 1400 30
7844 TURNER SALESMAN 7698 08-9月 -81 1500 0 30
7876 ADAMS CLERK 7788 23-5月 -87 1100 20
7934 MILLER CLERK 7782 23-1月 -82 1300 10
1 select *
2 from emp
3* where sal between 1000 and 200
SQL> /
2 查询10 20号部门的员工信息
查询不是10 20号部门的员工信息
select * from emp
where DEPTNO in (10, 20)
select * from emp
where DEPTNO not in (10, 20)
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- -------------- ---------- ---------- ----------
7369 SMITH CLERK 7902 17-12月-80 800 20
7566 JONES MANAGER 7839 02-4月 -81 2975 20
7782 CLARK MANAGER 7839 09-6月 -81 2450 10
7788 SCOTT ANALYST 7566 19-4月 -87 3000 20
7839 KING PRESIDENT 17-11月-81 5000 10
7876 ADAMS CLERK 7788 23-5月 -87 1100 20
7902 FORD ANALYST 7566 03-12月-81 3000 20
7934 MILLER CLERK 7782 23-1月 -82 1300 10
3 in 中是一个集合 .....null
select * from emp
where DEPTNO in (10, 20, null)
select * from emp
where DEPTNO not in (10, 20)
======> in集合中遇见null (in集合和空值在一起...)
=====>in (集合中含有空值 ) 查询结果不受影响
======>not in (集合中含有空值 ) 查询结果受影响
why? 思考题
=====> day下午....
4 like模糊查询
% 代表零个或多个字符(任意个字符)。
_ 代表一个字符。
查询名字S开头的员工信息
1 select * from emp
2* where ENAME like 'S%'
---''单引号中的字符串是区分大小写的....
SQL> /
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- -------------- ---------- ---------- ----------
7369 SMITH CLERK 7902 17-12月-80 800 20
7788 SCOTT ANALYST 7566 19-4月 -87 3000 20
查询名字含有4个字母的员工
select * from emp
where ENAME like '____'
1 select * from emp
2* where ENAME like '____'
SQL> /
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- -------------- ---------- ---------- ----------
7521 WARD SALESMAN 7698 22-2月 -81 1250 500 30
7839 KING PRESIDENT 17-11月-81 5000 10
7902 FORD ANALYST 7566 03-12月-81 3000 20
查询名字含有下划线的员工 (查询的内容含有转义字符)
insert into emp(empno, ename, sal , DEPTNO) values(1, 'tom_abc', 8000, 10)
select * from emp
where ename like '%\_%' escape '\'
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- -------------- ---------- ---------- ----------
1 tom_abc 8000 10
=================ORDER BY子句=============================
1 order by 默认按照排序 从小到大
2 order by后面+列名 表达式 别名 序号
按照薪水,查询员工信息
select *from emp
order by sal
select empno, ename, sal, sal*12 年薪 from emp
order by 年薪
1 select empno, ename, sal, sal*12 年薪 from emp
2* order by 年薪
SQL> /
EMPNO ENAME SAL 年薪
---------- ---------- ---------- ----------
7369 SMITH 800 9600
7900 JAMES 950 11400
7876 ADAMS 1100 13200
7521 WARD 1250 15000
7654 MARTIN 1250 15000
7934 MILLER 1300 15600
7844 TURNER 1500 18000
7499 ALLEN 1600 19200
7782 CLARK 2450 29400
7698 BLAKE 2850 34200
7566 JONES 2975 35700
7902 FORD 3000 36000
7788 SCOTT 3000 36000
7839 KING 5000 60000
1 tom_abc 8000 96000
按照年薪,查询员工信息
2 order by 遇上空值
1 任何表达式和null运算都为空
2 null != null (is null )
3 where子句中(in集合和空值在一起...)
4
按照奖金,查询员工信息
1 select empno, ename, comm from emp
2 order by 3 desc
3* nulls last
SQL> /
EMPNO ENAME COMM
---------- ---------- ----------
7654 MARTIN 1400
7521 WARD 500
7499 ALLEN 300
7844 TURNER 0
7782 CLARK
7788 SCOTT
7839 KING
7876 ADAMS
7900 JAMES
7902 FORD
7698 BLAKE
7566 JONES
7934 MILLER
1 tom_abc
7369 SMITH
已选择15行。
3 order by 后面有多个列,
如果多个列中 有desc修饰,只作用于最近的那一列
按照部门排序
select * from emp
order by deptno desc, sal desc;
按照部门和工资排序
select * from emp
order by deptno desc, sal desc
/
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- -------------- ---------- ---------- ----------
7698 BLAKE MANAGER 7839 01-5月 -81 2850 30
7499 ALLEN SALESMAN 7698 20-2月 -81 1600 300 30
7844 TURNER SALESMAN 7698 08-9月 -81 1500 0 30
7521 WARD SALESMAN 7698 22-2月 -81 1250 500 30
7654 MARTIN SALESMAN 7698 28-9月 -81 1250 1400 30
7900 JAMES CLERK 7698 03-12月-81 950 30
7902 FORD ANALYST 7566 03-12月-81 3000 20
7788 SCOTT ANALYST 7566 19-4月 -87 3000 20
7566 JONES MANAGER 7839 02-4月 -81 2975 20
7876 ADAMS CLERK 7788 23-5月 -87 1100 20
7369 SMITH CLERK 7902 17-12月-80 800 20
1 tom_abc 8000 10
7839 KING PRESIDENT 17-11月-81 5000 10
7782 CLARK MANAGER 7839 09-6月 -81 2450 10
7934 MILLER CLERK 7782 23-1月 -82 1300 10
已选择15行。