Oracle笔记系列(二)
来源:互联网 发布:二元期权模拟软件 编辑:程序博客网 时间:2024/04/30 20:10
(2)过滤和排序SQL> -过滤:where子句SP2-0734: 未知的命令开头 "-过滤:whe..." - 忽略了剩余的行。SQL> --过滤:where子句SQL> host clsSQL> --查询部门号为10的员工信息SQL> select * from emp where deptno=10;SQL> --查询姓名叫KING的员工SQL> select * from emp where ename='KING';SQL> /*select * from emp where ename='king';*/SQL> select * from emp where ename='king';SQL> -- MYSQL中试试SQL> --字符串大小写敏感SQL> --日期:SQL> --查询入职日期为17-11月-81的员工SQL> select * from emp where hiredate='17-11月-81'; SQL> select * from emp where hiredate='1981-11-17'; 第 3 行出现错误: ORA-01861: 文字与格式字符串不匹配 SQL> --对于日期,格式敏感SQL> --如何获取当前系统的日期格式?SQL> --1.查询系统时间SQL> select sysdate from dual;SYSDATE -------------- 27-9月 -11 已选择 1 行。SQL> --sysdate:既包含日期,也包含时间SQL> --2. 查询系统的参数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_DUAL_CURRENCY ¥ NLS_NCHAR_CHARACTERSET AL16UTF16 NLS_COMP BINARY SQL> --查询名字是4个字的员工SQL> select * 2 from emp 3 where ename like '____';SQL> insert into emp(empno,ename) values(1234,'Tom_123');SQL> select * from emp;SQL> --查询名字中含义下划线的员工SQL> select * 2 from emp 3 where ename like '%_%';SQL> --转义字符,选取%和_SQL> ed已写入 file afiedt.buf 1 select * 2 from emp 3* where ename like '%\_%' escape '\'SQL> ed已写入 file afiedt.buf 1 select * 2 from emp 3* where ename like '%a_%' escape 'a'SQL> rollback;回退已完成。SQL> --排序 order bySQL> --查询员工的信息,按照工资排序SQL> --a命令:追加命令SQL> a order by sal 2* from emporder by salSQL> --a:两个以上的空格SQL> a order by sal; 1* select * from emp order by salSQL> /SQL> a desc 1* select * from emp order by sal descSQL> /*SQL> order by后面可以跟什么:SQL> 1. 列名2. 表达式3. 别名4. 序号SQL> */SQL> --查询员工信息,按照年薪排序SQL> select empno,ename,sal,sal*12 2 from emp 3 order by sal*12;SQL> --按照别名排序SQL> select empno,ename,sal,sal*12+nvl(comm,0) 年收入 2 from emp 3 order by 年收入; EMPNO ENAME SAL 年收入 ---------- -------- ----- ---------- 7369 SMITH 800 9600
SQL> --修改日期格式SQL> alter session set NLS_DATE_FORMAT='yyyy-mm-dd';会话已更改。SQL> select * 2 from emp 3 where hiredate='1981-11-17'; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- -------- --------- ---------- ---------- ----- 7839 KING PRESIDENT 1981-11-17 5000 10 已选择 1 行。SQL> alter session set NLS_DATE_FORMAT='DD-MON-RR';会话已更改。SQL> host clsSQL> --between.. and: 在某个区间上SQL> -- 查询薪水在1000~2000的员工SQL> select * from emp where sal>=1000 and sal<=2000;SQL> select * from emp where sal between 1000 and 2000;SQL> --问题:between..and含边界吗? 含SQL> select * from emp where sal between 2000 and 1000;未选定行SQL> --between and: 小值在前,大值在后SQL> host clsSQL> --in:在集合中SQL> --查询部门号为10,20的员工SQL> select * from emp where deptno=10 or deptno=20;SQL> select * from emp where deptno in (10,20);SQL> --方式三:SQL> /*SQL> select * from emp where deptno=10SQL> +SQL> select * from emp where deptno=20;SQL> */SQL> host clsSQL> --like:模糊查询SQL> --查询名字以S打头的员工SQL> select * 2 from emp 3 where ename like 'S%';
SQL> ed已写入 file afiedt.buf 1 select empno,ename,sal,sal*12+nvl(comm,0) 年收入 2 from emp 3* order by 4SQL> / EMPNO ENAME SAL 年收入 ---------- -------- ----- ---------- 7369 SMITH 800 9600 7900 JAMES 950 11400 7876 ADAMS 1100 13200 7521 WARD 1250 15500 SQL> --order by后面有多列,怎么排序?SQL> select * 2 from emp 3 order by deptno,sal;SQL> --先按照第一列排;如果相同,再按照第二列排;以此类推SQL> ed已写入 file afiedt.buf 1 select * 2 from emp 3* order by deptno,sal descSQL> ed已写入 file afiedt.buf 1 select * 2 from emp 3* order by deptno desc,sal descSQL> /SQL> --作用于多列时,降序需要在每列后SQL> host clsSQL> --查询员工信息,按照奖金排序SQL> select * 2 from emp 3 order by comm;SQL> ed已写入 file afiedt.buf 1 select * 2 from emp 3* order by comm descSQL> /SQL> --如何将空值排到最后SQL> select * 2 from emp 3 order by comm desc 4 nulls last;