Oracle(八)老师

6 篇文章 0 订阅
6 篇文章 0 订阅

Dept表

 名称              是否为空? 类型
 ----------------- -------- ------------
 DEPTNO                     NUMBER(2)
 DNAME                      VARCHAR2(14)
 LOC                        VARCHAR2(13)

salgrade表

 名称              是否为空? 类型
----------------- -------- ------------
GRADE                      NUMBER
LOSAL                      NUMBER
HISAL                      NUMBER

 

多表查询

多表链接 首先做到的是笛卡尔乘积

A表

a1 a2 a3

b1 b2 b3

c1  c2 c3

B

d1 d2

f1 f2

结果

a1 a2 a3 d1 d2

b1 b2 b3 d1 d2

c1  c2 c3 d1 d2

a1 a2 a3 f1 f2

b1 b2 b3 f1 f2

c1  c2 c3 f1 f2

结果分析:

获得的行数:A*B

获得的列数:A+B

 --------------------------------------------------------------

两个表合成一个表 中间加个条件

emp,dept 相连

emp.sal>salgrade.losal and emp.sal<salgrade.hisal(between losal and hisal)

emp,dept,salgrade三表相连

select * from emp,dept,salgrade where emp.deptno=dept.deptno and (emp.sal>salgrade.losal and

emp.sal<salgrade.hisal)

 

其他函数

控制替换 nvl(comm.0);

例:sal 和 comm 的和  如果之间有空值 直接加的话 空值依然是空值

select nvl(sal,0)+nvl(comm,0) from emp;把空值替换成0 在进行计算

 

 如果想找管理者的话 需要关联 另一张表 所以就创建一张表

 select emp.empno,manager.mgr from emp,manager where emp.empno=manager.mgr

这里我们可以用别名 从而访问同一张表(别名的意思 也就是虚拟一张自己)

select e.empno,m.mgr from emp e.manager m where e.empno=m.mgr;

练习:查询 和 allen 在同一个工资级别的员工信息;

select * from emp,salgrade where sal between losal and hisal and grade=(select grade from emp,salgrade where sal between losal and hisal and ename='ALLEN')

---------------------------------------------------------------------------------------------------------------------

多表查询
多表连接首先做的是笛卡尔乘积
A
a1 a2 a3
b1 b2 b3
c1 c2 c3

B
d1 d2
f1 f2
C
g1 g1
h1 h2

结果
a1 a2 a3 d1 d2 g1 g1
b1 b2 b3 d1 d2 g1 g1
c1 c2 c3 d1 d2 g1 g1
a1 a2 a3 f1 f2 g1 g1
b1 b2 b3 f1 f2 g1 g1
c1 c2 c3 f1 f2 g1 g1
a1 a2 a3 d1 d2 h1 h2
b1 b2 b3 d1 d2 h1 h2
c1 c2 c3 d1 d2 h1 h2
a1 a2 a3 f1 f2 h1 h2
b1 b2 b3 f1 f2 h1 h2
c1 c2 c3 f1 f2 h1 h2
结果分析:
获得的行数:A*B*C
获得的列数:A+B+C


多表连接类型
  等值连接
 非等值连接
 自连接 --自己连接自己 在使用自连接时必须使用表别名

nvl函数

表别名


子查询:查询的语句中还包含查询,内部的查询就是子查询
使用子查询:当要查询一个复杂内容 通过一个简单的查询无法实现,这时使用子查询


在使用子查询时
根据返回的值的不同使用的符号也不同
当返回一个值的时候使用 = > < >= <=
当返回一列值时使用 in  any  all     any 和all在使用时必须和基本符号一起使用
>any  <any =any >=any >=all

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值