—多表联合查询:
–当需要获取的数据分布在多张中,考虑使用联合查询
–SQL92方式
–SQL99方式
–SQL92方式
–笛卡尔积:将多个表的数据进行一一对应,所得到结果为多表的笛卡尔积。
–结果的数量为所有表的数量的乘积。
select * from emp,dept where emp.deptno=dept.deptno
–等值连接筛选
–概念:先做表的笛卡尔积,然后筛选,筛选条件为等值筛选。
–注意:条件为字段的值相同来进行筛选,字段的名字可以不同
–查询员工姓名,工作,薪资,部门名称
select * from emp,dept where emp.deptno=dept.deptno
–可以直接在select子句中使用字段直接获取数据,但是效率比较低。建议字段前加上表名
–注意:如果是公共字段,则必须声明表名
select ename,job,sal,dname from emp,dept where emp.deptno=dept.deptno–等值连接筛选
select emp.ename,emp.job,emp.sal,dept.dname,emp.deptno from emp,dept where emp.deptno=dept.deptno
select e.ename,e.job,e.sal,d.dname from emp e,dept d where e.deptno=d.deptno and sal>2000–给表使用别名
–不等值连接
–查询员工姓名,工作,工资,工资等级
select * from emp e,salgrade s where e.sal>=s.losal and e.sal<=s.hisal
–自连接:
–查询员工姓名,工作,薪资,及上级领导姓名
select e1.ename,e1.job,e1.sal,e2.ename from emp e1,emp e2 where e1.mgr=e2.empno
–外连接
–左外连接:加在右边,显示左边对应字段没有值的数据
–查询员工姓名,工作,薪资,部门名称及没有部门的员工信息
select * from emp e,dept d where e.deptno=d.deptno(+)
–右外连接:加在做边,显示右边边对应字段没有值的数据
–查询员工姓名,工作,薪资,部门名称及没有员工信息的部门
select * from emp e,dept d where e.deptno(+)=d.deptno