数据库学习(三)多表联查
@
目录
一、99语法
Oracle学习(二)中我们学习了92语法,现在我们学习一下99语法
sql 1999语法
1.1.cross join 笛卡尔积
select * from emp cross join dept;
1.2.natural join 自然连接
当两个表不具有相同列名,进行cross join,具有相同列名,自动匹配
select * from emp e natural join dept d;
1.3.on子句,添加链接条件,相当于92语法的等值连接
相当于92语法的等值连接
select * from emp e join dept d on e.deptno=d.deptno
相当于92语法的非等值连接
select * from emp e join salgrade sg on e.sal between sg.losal and sg.hisal
1.4.left outer join
左表中数据全部显示,游标没有数据直接显示空
select * from emp left outer join dept d on e.deptno=d.deptno
1.5.right outer join
右表全部显示,没有数据显示空
select * from emp right outer join dept d on e.deptno=d.deptno
1.6.full outer join
select * from emp full outer join dept d on e.deptno=d.deptno
1.7.inner outer join
inner join 和 join一样
select * from emp e inner outer join dept d on e.deptno=d.deptno
1.8.using
除了使用on表示连接条件,也可以使用using
使用using会少一列,因为deptno为连接列,不归属任何一张表
select * from emp e join dept d using(deptno)
二、至少两种方式行转列
| 姓名 | 语文 | 数学 | 英语 |
|---|---|---|---|
| 王五 | 88 | 99 | 50 |
2.1.decode
select ss.name,
max(decode(ss.subject,'语文',ss.score)) 语文,
max(decode(ss.subject,'数学',ss.score)) 数学,
max(decode(ss.subject,'英语',ss.score)) 英语,
from student_score ss group by ss.name
2.2.case when then
select ss.name,
max(case ss.subject
when '语文' then ss.score
end) 语文,
max(case ss.subject
when ‘数学’ then ss.score
end) 数学,
max(case ss.subject
when '英语' then ss.score
end) 英语
from student_score ss group by ss.name
2.3.join
select ss01.name,ss01.score 语文,ss02.score 数学,ss03.score 英语
from(select ss.name,ss.score
from student_score as
where ss.subject='语文') ss01
join(select ss.name,ss.score
from student_score as
where ss.subject='数学') ss02
join(select ss.name,ss.score
from student_score as
where ss.subject='英语') ss03
on ss01.name = ss03.name;
三、分页
必须内部放入一个字段,否则rn是可活动的
select * from (select emp.*,rownum rn from emp) where rn>2 and rn <5
本文深入探讨了数据库中各种联接操作,包括交叉联接、自然联接、外键联接等,并介绍了如何利用decode、case when以及join实现行到列的数据转换,最后讲解了分页查询的方法。
1162

被折叠的 条评论
为什么被折叠?



