首先介绍一下spool命令
当你要一个大的报表时
spool 路径:打开一个spool
写完后
spool off: 关闭spool
在相应的路径下就有你要的文件了。
SQL> spool E:\spooll
SQL> select empno,ename,job,sal
2 from emp
3 where sal >=1500
4 order by job, sal DESC;
EMPNO ENAME JOB SAL
---------- ---------- --------- ----------
7788 SCOTT ANALYST 3000
7902 FORD ANALYST 3000
7566 JONES MANAGER 2975
7698 BLAKE MANAGER 2850
7782 CLARK MANAGER 2450
7839 KING PRESIDENT 5000
7499 ALLEN SALESMAN 1600
7844 TURNER SALESMAN 1500
已选择8行。
SQL> spool off
--------------------
select ename from emp;
ename 是个列,这个叫投影
一个完整的sql语句叫statement ,每一个关键字和后面跟着的选项叫子句。
sqlplus默认的列标题结果显示是:
字符和日期型数据为左对齐
数字型数据为右对齐
SQL> select hiredate
2 from emp;
HIREDATE
--------------
17-12月-80
20-2月 -81
22-2月 -81
02-4月 -81
28-9月 -81
01-5月 -81
09-6月 -81
19-4月 -87
17-11月-81
08-9月 -81
23-5月 -87
HIREDATE
--------------
03-12月-81
03-12月-81
23-1月 -82
已选择14行。
SQL> select comm
2 from emp;
COMM
----------
300
500
1400
0
COMM
----------
已选择14行。
----
sql语句中使用表达式,表达式中又有 + - * / 4种运算符 加,减,乘,除
除了from子句外,他们都可以出现,因为from子句只能使用表,或视图
当然他们之间也是有优先级的:从左到右,先乘除后加减,及括号()优先
=======
sql中使用别名
ename as “ ”
是的,使用“”,当然你可以不写“”,但推荐写
select ename as " name 'from emp;
使用别名是因为大部分列名用的都是大写(缩写) 但不好阅读。
====
在学linux时,我经常用管道命令,||
而在sql中,||用于连结两边的内容
select ename ||'aaaaa'||(sal+500)*12 "title"
‘xxxx’如果想输出‘,单引号则用 q'[ ' ]'
最后的“”中的内容为标题,列标
我省略了 as
SQL> l
1 SELECT ename||'annual salary is'||(500+sal)*12 as title
2* from emp
SQL>
但是这样的话title会默认大写
也就是说“xxx”中的内容原样输出、
SQL> SELECT ename||'annual salary is '||(500+sal)*12 "titleaaaa"
2 from emp;
titleaaaa
-------------------------------------------------------------------
SMITHannual salary is 15600
ALLENannual salary is 25200
WARDannual salary is 21000
JONESannual salary is 41700
MARTINannual salary is 21000
BLAKEannual salary is 40200
CLARKannual salary is 35400
SCOTTannual salary is 42000
KINGannual salary is 66000
TURNERannual salary is 24000
ADAMSannual salary is 19200
titleaaaa
-------------------------------------------------------------------
JAMESannual salary is 17400
FORDannual salary is 42000
MILLERannual salary is 21600
----------------
distinct 去掉重复的行
SQL> SELECT deptno from emp;
DEPTNO
----------
20
30
30
20
30
30
10
20
10
30
20
DEPTNO
----------
30
20
10
已选择14行。
SQL> SELECT DISTINCT deptno from emp;
DEPTNO
----------
30
20
10
SQL>
但是应尽量避免使用distinct,因为oracle使用排序来发现重复的值,这样数据量大时会降低效率
推荐使用UNIQUE
unique 其结果完全相同。
SQL> SELECT UNIQUE deptno from emp;
DEPTNO
----------
30
20
10
SQL>
----------------------
-----------------
WHERE:
where:作用于表,
限制了那些行(记录)要显示,在sql中叫做选择
where后跟条件
但where子句中不能使用别名
select ename ,sal
from emp
where sal > 1500
SQL> SELECT ename sal
2 from emp
3 WHERE sal >1500;
SAL
----------
ALLEN
JONES
BLAKE
CLARK
SCOTT
KING
FORD
已选择7行。
SQL> SELECT ENAME SAL
2 WHERE SAL >1500
3 FROM EMP;
WHERE SAL >1500
*
第 2 行出现错误:
ORA-00923: 未找到要求的 FROM 关键字
是的,where只能写在from后
------
比较运算符;
expression operator expression
> , >= ,< ,<= ,= ,<> ,!=
a between ...... and......
....与。。。之间
一般都用在了where子句里
select empno,ename,sal
from emp
where sal between 1500 and 2000;
SQL> l
1 select empno,ename,sal
2 from emp
3 where sal between 1500 and 2000
4*
SQL> /
EMPNO ENAME SAL
---------- ---------- ----------
7499 ALLEN 1600
7844 TURNER 1500
SQL>
还有一种用法:
where a not between ... and ...;
表示不在他们之间
---
当然查询语句中可以 使用字符串
select empno, ename, job
from emp
where job='salesman'; 这样写是没东西展示的
SQL> l
1 select empno, ename, job
2 from emp
3 where job='salesman'
4*
SQL> /
未选定行
select empno, ename, job
from emp
where job='SALSMAN';
SQL> l
1 select empno, ename, job
2 from emp
3 where job='salesman'
4*
SQL> 3
3* where job='salesman'
SQL> 3 where job='SALESMAN'
SQL> /
EMPNO ENAME JOB
---------- ---------- ---------
7499 ALLEN SALESMAN
7521 WARD SALESMAN
7654 MARTIN SALESMAN
7844 TURNER SALESMAN
SQL> L
1 select empno, ename, job
2 from emp
3 where job='SALESMAN'
4*
SQL> /
EMPNO ENAME JOB
---------- ---------- ---------
7499 ALLEN SALESMAN
7521 WARD SALESMAN
7654 MARTIN SALESMAN
7844 TURNER SALESMAN
是的,换成大写就行。
------------
in 比较运算符,
select empno,ename,sal,job
from emp
where job in ('SALESMAN','CLERK','MANAGER');
当然有个用法 not in()
-------
模糊查询 like
where job like 'SAL%'
%代表0个或是多个字符
-代表一个字符,只有一个哦
如果要查询的字符里就有 - %就得转义
’\%' escape '\'
转义字符是\
自定义 ~
'~%' escape '~';
-------------
order by子句
首先:
ASC(ascending order) 升序 默认
DESC(descending order) 降序
用作对象 日期,字符,数字的升序降序。
order by 子句,一定是放最后的语句。
SQL> select empno,ename,sal
2 from emp
3 where sal>=1500
4 order by sal DESC;
EMPNO ENAME SAL
---------- ---------- ----------
7839 KING 5000
7902 FORD 3000
7788 SCOTT 3000
7566 JONES 2975
7698 BLAKE 2850
7782 CLARK 2450
7499 ALLEN 1600
7844 TURNER 1500
已选择8行。
不使用 order by的话就是乱序。
--使用别名
select empno as "employee number",ename,sal
from emp
order by "employee number";
SQL> select empno as "employee number",ename,sal from emp order by "employee number";
employee number ENAME SAL
--------------- ---------- ----------
7369 SMITH 800
7499 ALLEN 1600
7521 WARD 1250
7566 JONES 2975
7654 MARTIN 1250
7698 BLAKE 2850
7782 CLARK 2450
7788 SCOTT 3000
7839 KING 5000
7844 TURNER 1500
7876 ADAMS 1100
employee number ENAME SAL
--------------- ---------- ----------
7900 JAMES 950
7902 FORD 3000
7934 MILLER 1300
已选择14行。
也可以使用表达式;
SELECT empno as "employee number",ename name,(500+sal)*12 as "annual salary"
2 from emp
3 order by (500+sal)*12 DESC;
SQL> SELECT empno as "employee number",ename name,(500+sal)*12 as "annual salary"
2 from emp
3 order by (500+sal)*12 DESC;
employee number NAME annual salary
--------------- ---------- -------------
7839 KING 66000
7902 FORD 42000
7788 SCOTT 42000
7566 JONES 41700
7698 BLAKE 40200
7782 CLARK 35400
7499 ALLEN 25200
7844 TURNER 24000
7934 MILLER 21600
7521 WARD 21000
7654 MARTIN 21000
employee number NAME annual salary
--------------- ---------- -------------
7876 ADAMS 19200
7900 JAMES 17400
7369 SMITH 15600
按列号排序:order by 3 DESC;
第3个查询排序,只是少打点字
不推荐使用
SQL> l
1 SELECT empno as "employee number",ename name,(500+sal)*12 as "annual salary"
2 from emp
3* order by 3 DESC
SQL> /
employee number NAME annual salary
--------------- ---------- -------------
7839 KING 66000
7902 FORD 42000
7788 SCOTT 42000
7566 JONES 41700
7698 BLAKE 40200
7782 CLARK 35400
7499 ALLEN 25200
7844 TURNER 24000
7934 MILLER 21600
7521 WARD 21000
7654 MARTIN 21000
employee number NAME annual salary
--------------- ---------- -------------
7876 ADAMS 19200
7900 JAMES 17400
7369 SMITH 15600
-------------
order by job, sal DESC;
表示 job 为降序,sal为升序,
且先按job排序,再在每一组里拍序
SQL> select ename,job,sal
2 from emp
3 order by job,sal DESC;
ENAME JOB SAL
---------- --------- ----------
FORD ANALYST 3000
SCOTT ANALYST 3000
MILLER CLERK 1300
ADAMS CLERK 1100
JAMES CLERK 950
SMITH CLERK 800
JONES MANAGER 2975
BLAKE MANAGER 2850
CLARK MANAGER 2450
KING PRESIDENT 5000
ALLEN SALESMAN 1600
ENAME JOB SAL
---------- --------- ----------
TURNER SALESMAN 1500
MARTIN SALESMAN 1250
WARD SALESMAN 1250
当然你可以使用不在select 子句中的列,放在order by里,但是不推荐这样做,可读性差。