(九)逻辑运算,order by,desc

时间:2022-11-12 23:39:11

逻辑运算

AND,OR,NOT

......where 表达式1  and 表达式2;

......where 表达式2  and 表达式1;

SQL优化:

  SQL在解析where时是从右向左解析的。所以:and 时应该将易假的放在右侧,or时应该将易真的值放在右侧

order by

order by 之后可以跟哪些内容呢?

order by + 列名,表达式 ,别名,序号

SQL> select ename,sal,sal*12 "年薪",sal+nvl(comm,0) from emp order by 2;

ENAME             SAL       年薪 SAL+NVL(COMM,0)
---------- ---------- ---------- ---------------
SMITH 800 9600 800
JAMES 950 11400 950
ADAMS 1100 13200 1100
WARD 1250 15000 1750
MARTIN 1250 15000 2650
MILLER 1300 15600 1300
TURNER 1500 18000 1500
ALLEN 1600 19200 1900
CLARK 2450 29400 2450
BLAKE 2850 34200 2850
JONES 2975 35700 2975 ENAME SAL 年薪 SAL+NVL(COMM,0)
---------- ---------- ---------- ---------------
SCOTT 3000 36000 3000
FORD 3000 36000 3000
KING 5000 60000 5000 已选择14行。 SQL> set pagesize 100;
SQL> set timing on;
SQL> ed
已写入 file afiedt.buf 1* select ename,sal,sal*12 "年薪",sal+nvl(comm,0) from emp order by "年薪"
SQL> / ENAME SAL 年薪 SAL+NVL(COMM,0)
---------- ---------- ---------- ---------------
SMITH 800 9600 800
JAMES 950 11400 950
ADAMS 1100 13200 1100
WARD 1250 15000 1750
MARTIN 1250 15000 2650
MILLER 1300 15600 1300
TURNER 1500 18000 1500
ALLEN 1600 19200 1900
CLARK 2450 29400 2450
BLAKE 2850 34200 2850
JONES 2975 35700 2975
SCOTT 3000 36000 3000
FORD 3000 36000 3000
KING 5000 60000 5000 已选择14行。 已用时间: 00: 00: 00.08
SQL> ed
已写入 file afiedt.buf 1* select ename,sal,sal*12 "年薪",sal+nvl(comm,0) from emp order by sal+nvl(comm,0) desc
SQL> / ENAME SAL 年薪 SAL+NVL(COMM,0)
---------- ---------- ---------- ---------------
KING 5000 60000 5000
FORD 3000 36000 3000
SCOTT 3000 36000 3000
JONES 2975 35700 2975
BLAKE 2850 34200 2850
MARTIN 1250 15000 2650
CLARK 2450 29400 2450
ALLEN 1600 19200 1900
WARD 1250 15000 1750
TURNER 1500 18000 1500
MILLER 1300 15600 1300
ADAMS 1100 13200 1100
JAMES 950 11400 950
SMITH 800 9600 800 已选择14行。 已用时间: 00: 00: 00.13
SQL>

order 后有多列时,列名之间用逗号隔开,order by 会同时作用于多列,如下例:会在同一部门内升序,部门间再升序

SQL> set linesize 140;
SQL> select * from emp order by deptno,sal; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- -------------- ---------- ---------- ----------
7934 MILLER CLERK 7782 23-1月 -82 1300 10
7782 CLARK MANAGER 7839 09-6月 -81 2450 10
7839 KING PRESIDENT 17-11月-81 5000 10
7369 SMITH CLERK 7902 17-12月-80 800 20
7876 ADAMS CLERK 7788 23-5月 -87 1100 20
7566 JONES MANAGER 7839 02-4月 -81 2975 20
7788 SCOTT ANALYST 7566 19-4月 -87 3000 20
7902 FORD ANALYST 7566 03-12月-81 3000 20
7900 JAMES CLERK 7698 03-12月-81 950 30
7654 MARTIN SALESMAN 7698 28-9月 -81 1250 1400 30
7521 WARD SALESMAN 7698 22-2月 -81 1250 500 30
7844 TURNER SALESMAN 7698 08-9月 -81 1500 0 30
7499 ALLEN SALESMAN 7698 20-2月 -81 1600 300 30
7698 BLAKE MANAGER 7839 01-5月 -81 2850 30 已选择14行。 已用时间: 00: 00: 00.14
SQL>

desc 只作用于最近的一列,两列都降序,需要两个desc

SQL> select * from emp order by deptno,sal desc;

     EMPNO ENAME      JOB              MGR HIREDATE              SAL       COMM     DEPTNO
---------- ---------- --------- ---------- -------------- ---------- ---------- ----------
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
7788 SCOTT ANALYST 7566 19-4月 -87 3000 20
7902 FORD ANALYST 7566 03-12月-81 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
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
7654 MARTIN SALESMAN 7698 28-9月 -81 1250 1400 30
7521 WARD SALESMAN 7698 22-2月 -81 1250 500 30
7900 JAMES CLERK 7698 03-12月-81 950 30 已选择14行。 已用时间: 00: 00: 00.12
SQL> 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
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 已选择14行。 已用时间: 00: 00: 00.14
SQL>

按奖金由高到低

 select * from emp order by comm desc,结果前面的值为NULL,数据在后面,应该将NULL放在后面,即:select * from emp order by comm desc nulls last;

已用时间:  00: 00: 00.14
SQL> select * from emp order by comm; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- -------------- ---------- ---------- ----------
7844 TURNER SALESMAN 7698 08-9月 -81 1500 0 30
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
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
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
7698 BLAKE MANAGER 7839 01-5月 -81 2850 30
7566 JONES MANAGER 7839 02-4月 -81 2975 20
7369 SMITH CLERK 7902 17-12月-80 800 20
7782 CLARK MANAGER 7839 09-6月 -81 2450 10 已选择14行。 已用时间: 00: 00: 00.13
SQL> select * from emp order by comm desc; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- -------------- ---------- ---------- ----------
7369 SMITH CLERK 7902 17-12月-80 800 20
7782 CLARK MANAGER 7839 09-6月 -81 2450 10
7902 FORD ANALYST 7566 03-12月-81 3000 20
7900 JAMES CLERK 7698 03-12月-81 950 30
7876 ADAMS CLERK 7788 23-5月 -87 1100 20
7566 JONES MANAGER 7839 02-4月 -81 2975 20
7698 BLAKE MANAGER 7839 01-5月 -81 2850 30
7934 MILLER CLERK 7782 23-1月 -82 1300 10
7788 SCOTT ANALYST 7566 19-4月 -87 3000 20
7839 KING PRESIDENT 17-11月-81 5000 10
7654 MARTIN SALESMAN 7698 28-9月 -81 1250 1400 30
7521 WARD SALESMAN 7698 22-2月 -81 1250 500 30
7499 ALLEN SALESMAN 7698 20-2月 -81 1600 300 30
7844 TURNER SALESMAN 7698 08-9月 -81 1500 0 30 已选择14行。 已用时间: 00: 00: 00.11
SQL> ed
已写入 file afiedt.buf 1* select * from emp order by comm desc
SQL> select * from emp order by comm desc nulls last; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- -------------- ---------- ---------- ----------
7654 MARTIN SALESMAN 7698 28-9月 -81 1250 1400 30
7521 WARD SALESMAN 7698 22-2月 -81 1250 500 30
7499 ALLEN SALESMAN 7698 20-2月 -81 1600 300 30
7844 TURNER SALESMAN 7698 08-9月 -81 1500 0 30
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
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
7698 BLAKE MANAGER 7839 01-5月 -81 2850 30
7566 JONES MANAGER 7839 02-4月 -81 2975 20
7369 SMITH CLERK 7902 17-12月-80 800 20
7782 CLARK MANAGER 7839 09-6月 -81 2450 10 已选择14行。 已用时间: 00: 00: 00.14
SQL>