Oracle多表查询
来源:互联网 发布:狼雨seo网站排名 编辑:程序博客网 时间:2024/05/19 18:45
笛卡儿积 就行两表相乘
oracle 表连接分为 等值连接 不等值连接 外连接 自连接
SQL> --等值连接SQL> --查询员工信息: 员工号 姓名 月薪 部门名称SQL> select empno,ename,sal,dname 2 from emp,dept 3 where emp.deptno=dept.deptno;SQL> ed已写入 file afiedt.buf 1 select empno,ename,sal,dname 2 from emp e,dept d 3* where e.deptno=d.deptnoSQL> /SQL> --不等值连接SQL> --查询员工信息: 姓名 月薪 工资级别SQL> select ename,sal,grade 2 from emp e,salgrade s 3 where e.sal between s.losal and s.hisal;SQL> --外连接SQL> --按部门统计员工人数: 部门号 部门名称 人数SQL> select d.deptno,d.dname,count(e.empno) 2 from dept d,emp e 3 where d.deptno=e.deptno 4 group by d.deptno,d.dname;SQL>--以上结果部队 SQL> /*SQL> 外连接解决的问题:当条件不成立时,任然希望在结果中包含不成立的记录SQL> 左外连接: where d.deptno=e.deptno 当不成立时,等号左边代表的表的信息任然被包含SQL> 写法: where d.deptno=e.deptno(+)SQL> 右外连接: where d.deptno=e.deptno 当不成立时,等号右边代表的表的信息任然被包含SQL> 写法:where d.deptno(+)=e.deptnoSQL> */SQL> select d.deptno,d.dname,count(e.empno) 2 from dept d,emp e 3 where d.deptno=e.deptno(+) 4 group by d.deptno,d.dname; DEPTNO DNAME COUNT(E.EMPNO) ---------- -------------- -------------- 10 ACCOUNTING 3 40 OPERATIONS 0 20 RESEARCH 5 30 SALES 6 SQL> select d.deptno,d.dname,count(e.empno) 2 from dept d left join emp e ON d.deptno=e.deptno 3 group by d.deptno,d.dname;SQL> ed已写入 file afiedt.buf 1 select d.deptno,d.dname,count(e.empno) 2 from dept d,emp e 3 where d.deptno=e.deptno(+) 4 group by d.deptno,d.dname 5* order by 1SQL> / DEPTNO DNAME COUNT(E.EMPNO) ---------- -------------- -------------- 10 ACCOUNTING 3 20 RESEARCH 5 30 SALES 6 40 OPERATIONS 0 SQL> --自连接SQL> --查询员工信息:***的老板是***SQL> select e.ename||'的老板是'||b.ename 2 from emp e,emp b 3 where e.mgr=b.empno;SQL> --自连接:利用表的别名,将同一张表视为多张表SQL> --层次查询SQL> --自连接不太适合操作大表
层次查询
SQL> --层次查询: 对同一张表的前后两次操作,进行连接SQL> select level,empno,ename,mgr 2 from emp 3 connect by prior empno=mgr 4 start with mgr is null 5 order by 1;
适用场景类似于树 一对多 比如 中国-》省份》城市
0 0
- Oracle多表查询
- Oracle多表查询
- Oracle多表查询
- Oracle多表查询
- oracle多表查询
- Oracle:多表查询
- oracle多表查询
- Oracle多表查询
- Oracle多表查询
- oracle 多表查询
- oracle 多表查询
- Oracle 多表查询
- oracle多表查询
- oracle多表查询
- oracle多表查询
- oracle 多表查询
- Oracle多表查询
- Oracle 多表查询
- Android应用程序组件间通信(一)——Intent类简介
- [HighCharts] 关于对几组不同数量级的数据进行分类比较处理办法
- 深入理解jQuery中$.get、$.post、$.getJSON和$.ajax的用法
- Spring数据校验
- leetcode Plus One 1.5 难度系数1
- Oracle多表查询
- YiiMongoDbSuite
- Wordpress页面修改 去掉Just another WordPress site
- Effective JavaScript 读书笔记 1 严格模式
- 数学之路-群体行为与群体智能(6)
- 考试系统优化——准备工作
- Hibernate笔记:HQL查询总结(二)——条件查询(转)
- C++ map的基本操作和使用
- myeclipse快捷键最新版本