清屏指令:DOS窗口进 :clear SCR;
设置某个字段的宽度:
字符:
col keyword for a10;
数字
col length for 999999;
多表连接查询
1.交叉连接(笛卡尔积):所有情况的组合
select*from emp,dept ;(没意义)2.内连接
多张表通过相同字段进行匹配,只显示匹配成功的
a. 等值连接
select*from emp e,dept d where e.deptno = d.deptno;
b.select*from emp e innerjoin dept d on e.deptno = d.deptno;3.外连接
左外连接:以左表为基准(左表数据全部显示),去匹配右表数据,如果匹配成功 则全部显示,匹配不成功,显示部分(无数据部分,NULL填充)
a.select*from emp e leftouterjoin dept d on e.deptno = d.deptno;
b.Oracle独有
select*from emp e,dept d where e.deptno = d.deptno(+);
右外连接:以右表为基准(右表数据全部显示),去匹配左表数据,如果匹配成功 则全部显示,匹配不成功,显示部分(无数据部分,NULL填充)
a.select*from emp e rightouterjoin dept d on e.deptno = d.deptno;
b.Oracle独有
select*from emp e,dept d where e.deptno(+)= d.deptno;
全外连接: = 左外 + 右外 - 去重
自连接 :将一张表 通过别名 “视为” 不同的表
必须费性能
优化:
层次连接 : 效率高,难度大
SQL>selectlevel,empno,ename,mgr from emp
2connectby prior empno=mgr
3startwith mgr isnull4orderbylevel;
查询员工姓名以及该员工的领导姓名
select e.ename,b.ename from emp e,emp b where e.mgr=b.empno;
子查询:
1.子查询可以出现的位置:where、select、having、from;不能写在groupby 后面
a.select 单行列(常量列)
b.having
查询最低工资比10号部门的最低工资高的部门编号
select deptno,min(sal)from emp groupby deptno havingmin(sal)>(selectmin(sal)from emp where deptno =10);
c.from: 相当于修改了表结构(临时表)select*from(select ename , sal , sal *12from emp);2.主查询和子查询可以是,也可以不是同一张表
a.查询销售部的员工信息
select*from emp where deptno =(select deptno from dept where dname ='SALES');3.子查询可以用 单行操作符 (<>) ,多行操作符( in )
any : 只要有一个
all : 全部都得满足
a.查询销售部,财务部的员工信息
select*from emp where deptno in(select deptno from dept where dname ='SALES'or dname ='ACCOUNTING');
b.查询工资比30部门中 任何其中一个员工高的员工信息
select*from emp where sal >(selectmin(sal)from emp where deptno =30);select*from emp where sal >any(select sal from emp where deptno =30);4. 子查询中的null
a.select*from emp where mgr in(7566,7698,null);select*from emp where mgr in(7566,7698);
b.select*from emp where mgr notin(7566,7698);select*from emp where mgr notin(7566,7698,null);NUll自身特性,!=NULL 无法查询任何数据
用 isnull,isnotnull
c.查询不是领导的员工信息(子查询中排除NULL)
select*from emp where empno notin(select mgr from emp where mgr isnotnull)5.一般不在子查询中排序
top-n排序