这些都是平时不常用,但效率很高的sql,在此将其记下,以备查询。
- SQL> select * from (select sal as salary,comm as commission from emp) where salary<5000;
- SALARY COMMISSION
- ---------- ----------
- 800
- 1600 300
- 1250 500
- 2975
- 1250 1400
- 2850
- 2450
- 3000
- 1500 0
- 1100
- 950
- SALARY COMMISSION
- ---------- ----------
- 3000
- 1300
- 已选择13行。
- SQL> select ename||' WORKS AS A' ||job as msg from emp where deptno=10;
- MSG
- ------------------------------
- CLARK WORKS AS AMANAGER
- KING WORKS AS APRESIDENT
- MILLER WORKS AS ACLERK
- SQL> select ename,sal,case when sal <= 2000 then 'UNDERPAID' when sal >= 4000 then 'OVERPAID' else 'OK' end as status from emp;
- ENAME SAL STATUS
- ---------- ---------- ---------
- SMITH 800 UNDERPAID
- ALLEN 1600 UNDERPAID
- WARD 1250 UNDERPAID
- JONES 2975 OK
- MARTIN 1250 UNDERPAID
- BLAKE 2850 OK
- CLARK 2450 OK
- SCOTT 3000 OK
- KING 5000 OVERPAID
- TURNER 1500 UNDERPAID
- ADAMS 1100 UNDERPAID
- ENAME SAL STATUS
- ---------- ---------- ---------
- JAMES 950 UNDERPAID
- FORD 3000 OK
- MILLER 1300 UNDERPAID
- 已选择14行。
从表中随机返回n条记录
- SQL> select * from (select ename,job from emp order by dbms_random.value()) where rownum<=5;
- ENAME JOB
- ---------- ---------
- WARD SALESMAN
- CLARK MANAGER
- TURNER SALESMAN
- SMITH CLERK
- JONES MANAGER
- select * from (select ename,job from emp order by dbms_random.value()) where rownum=5;
这样的写法是错误的,因为oracle不会直接去找rownum为5的数据,它是一条一条往后查询的,有了1,才能有2,一直到5为止。
查找某列值为空的所有行
- SQL> select * from emp where comm is null;
使用coalesce函数用实际的值来替换空值
- SQL> select coalesce(comm,0) from emp;
以下两名效果是一样的
- SQL> select ename,job,sal from emp where deptno = 10 order by sal desc;
- ENAME JOB SAL
- ---------- --------- ----------
- KING PRESIDENT 5000
- CLARK MANAGER 2450
- MILLER CLERK 1300
- SQL> select ename,job,sal from emp where deptno = 10 order by 3 desc;
- ENAME JOB SAL
- ---------- --------- ----------
- KING PRESIDENT 5000
- CLARK MANAGER 2450
- MILLER CLERK 1300
substr的用法
- substr( string, start_position, [ length ] )
- SQL> select ename,job from emp order by substr(job,length(job)-1,2);
- ENAME JOB
- ---------- ---------
- ALLEN SALESMAN
- WARD SALESMAN
- MARTIN SALESMAN
- TURNER SALESMAN
- JONES MANAGER
- CLARK MANAGER
- BLAKE MANAGER
- KING PRESIDENT
- SMITH CLERK
- JAMES CLERK
- ADAMS CLERK
- ENAME JOB
- ---------- ---------
- MILLER CLERK
- SCOTT ANALYST
- FORD ANALYST
- 已选择14行。
将数字转为#,再将#用空替换
- SQL> select ename||' '||deptno as data from emp order by replace(translate(data,'0123456789','##########'),'#','');
- DATA
- ---------------------------------------------------
- ADAMS 20
- ALLEN 30
- BLAKE 30
- CLARK 10
- FORD 20
- JAMES 30
- JONES 20
- KING 10
- MARTIN 30
- MILLER 10
- SCOTT 20
- DATA
- ---------------------------------------------------
- SMITH 20
- TURNER 30
- WARD 30
- 已选择14行。
将空值进行排序
- SQL> select ename,sal,comm from emp order by comm desc nulls first;
- ENAME SAL COMM
- ---------- ---------- ----------
- SMITH 800
- JONES 2975
- CLARK 2450
- BLAKE 2850
- SCOTT 3000
- KING 5000
- JAMES 950
- MILLER 1300
- FORD 3000
- ADAMS 1100
- MARTIN 1250 1400
- ENAME SAL COMM
- ---------- ---------- ----------
- WARD 1250 500
- ALLEN 1600 300
- TURNER 1500 0
- 已选择14行。
- SQL> select ename,sal,comm from emp order by comm desc nulls last;
- ENAME SAL COMM
- ---------- ---------- ----------
- MARTIN 1250 1400
- WARD 1250 500
- ALLEN 1600 300
- TURNER 1500 0
- SMITH 800
- JONES 2975
- JAMES 950
- MILLER 1300
- FORD 3000
- ADAMS 1100
- BLAKE 2850
- ENAME SAL COMM
- ---------- ---------- ----------
- CLARK 2450
- SCOTT 3000
- KING 5000
- 已选择14行。
- SQL> select ename,sal,comm from emp order by comm nulls last;
- ENAME SAL COMM
- ---------- ---------- ----------
- TURNER 1500 0
- ALLEN 1600 300
- WARD 1250 500
- MARTIN 1250 1400
- SMITH 800
- JONES 2975
- JAMES 950
- MILLER 1300
- FORD 3000
- ADAMS 1100
- BLAKE 2850
- ENAME SAL COMM
- ---------- ---------- ----------
- CLARK 2450
- SCOTT 3000
- KING 5000
- 已选择14行。
- SQL> select ename,sal,comm from emp order by comm nulls first;
- ENAME SAL COMM
- ---------- ---------- ----------
- SMITH 800
- JONES 2975
- CLARK 2450
- BLAKE 2850
- SCOTT 3000
- KING 5000
- JAMES 950
- MILLER 1300
- FORD 3000
- ADAMS 1100
- TURNER 1500 0
- ENAME SAL COMM
- ---------- ---------- ----------
- ALLEN 1600 300
- WARD 1250 500
- MARTIN 1250 1400
- 已选择14行。
将用case函数得到的值,做为查询和排序条件
- SQL> select ename,sal,job,comm from emp order by case when job = 'SALESMAN' then comm else sal end;
- ENAME SAL JOB COMM
- ---------- ---------- --------- ----------
- TURNER 1500 SALESMAN 0
- ALLEN 1600 SALESMAN 300
- WARD 1250 SALESMAN 500
- SMITH 800 CLERK
- JAMES 950 CLERK
- ADAMS 1100 CLERK
- MILLER 1300 CLERK
- MARTIN 1250 SALESMAN 1400
- CLARK 2450 MANAGER
- BLAKE 2850 MANAGER
- JONES 2975 MANAGER
- ENAME SAL JOB COMM
- ---------- ---------- --------- ----------
- SCOTT 3000 ANALYST
- FORD 3000 ANALYST
- KING 5000 PRESIDENT
- 已选择14行。
- SQL> select ename,sal,job,comm,case when job = 'SALESMAN' then comm else sal end as ordered from emp order by 5;
- ENAME SAL JOB COMM ORDERED
- ---------- ---------- --------- ---------- ----------
- TURNER 1500 SALESMAN 0 0
- ALLEN 1600 SALESMAN 300 300
- WARD 1250 SALESMAN 500 500
- SMITH 800 CLERK 800
- JAMES 950 CLERK 950
- ADAMS 1100 CLERK 1100
- MILLER 1300 CLERK 1300
- MARTIN 1250 SALESMAN 1400 1400
- CLARK 2450 MANAGER 2450
- BLAKE 2850 MANAGER 2850
- JONES 2975 MANAGER 2975
- ENAME SAL JOB COMM ORDERED
- ---------- ---------- --------- ---------- ----------
- SCOTT 3000 ANALYST 3000
- FORD 3000 ANALYST 3000
- KING 5000 PRESIDENT 5000
- 已选择14行。
本文转自sucre03 51CTO博客,原文链接:http://blog.51cto.com/sucre/414090,如需转载请自行联系原作者