标签:互斥 特定 结果 意义 sel big img 连接 clerk
通过两个表具有相同意义的列,建立连接条件。查询结果只显示两个列中的值是等值条件的行数据。表中同名列被选择时必须添加表名前缀进行修饰,否则无法确定这一列是属于哪个表。
按deptno的等值关系联合两张表的所有列
idle> select * from dept,emp where dept.deptno=emp.deptno;
DEPTNO DNAME LOC EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------- ------------- ---------- ---------- --------- ---------- ------------------- ---------- ---------- ----------
10 ACCOUNTING NEW YORK 7782 CLARK MANAGER 7839 1981-06-09 00:00:00 2450 10
10 ACCOUNTING NEW YORK 7839 KING PRESIDENT 1981-11-17 00:00:00 5000 10
10 ACCOUNTING NEW YORK 7934 MILLER CLERK 7782 1982-01-23 00:00:00 1300 10
20 RESEARCH DALLAS 7566 JONES MANAGER 7839 1981-04-02 00:00:00 2975 20
20 RESEARCH DALLAS 7902 FORD ANALYST 7566 1981-12-03 00:00:00 3000 20
20 RESEARCH DALLAS 7876 ADAMS CLERK 7788 1987-05-23 00:00:00 1100 20
20 RESEARCH DALLAS 7369 SMITH CLERK 7902 1980-12-17 00:00:00 800 20
20 RESEARCH DALLAS 7788 SCOTT ANALYST 7566 1987-04-19 00:00:00 3000 20
30 SALES CHICAGO 7521 WARD SALESMAN 7698 1981-02-22 00:00:00 1250 500 30
30 SALES CHICAGO 7844 TURNER SALESMAN 7698 1981-09-08 00:00:00 1500 0 30
30 SALES CHICAGO 7499 ALLEN SALESMAN 7698 1981-02-20 00:00:00 1600 300 30
30 SALES CHICAGO 7900 JAMES CLERK 7698 1981-12-03 00:00:00 950 30
30 SALES CHICAGO 7698 BLAKE MANAGER 7839 1981-05-01 00:00:00 2850 30
30 SALES CHICAGO 7654 MARTIN SALESMAN 7698 1981-09-28 00:00:00 1250 1400 30
14 rows selected.
当两个表中有相同的列名时,为了区分,要在列前加上表名作前缀。不冲突时,可以直接使用
idle> select deptno,empno,ename,dname,sal from emp a,dept b where b.deptno=a.deptno;
select deptno,empno,ename,dname,sal from emp a,dept b where b.deptno=a.deptno
*
ERROR at line 1:
ORA-00918: column ambiguously defined
就是排除完全相等条件以外的 >,<,!=, <=, >=, between and。主要在于不同表之间显示特定范围的信息(也可以理解成包含关系)
例如: SALGRADE表把工资划分了5个等级 idle> select * from SALGRADE; GRADE LOSAL HISAL ---------- ---------- ---------- 1 700 1200 2 1201 1400 3 1401 2000 4 2001 3000 5 3001 9999 idle> 查处每个员工的工资等级 idle> select ename,sal,grade from emp a,salgrade b where sal between losal and hisal; ENAME SAL GRADE ---------- ---------- ---------- SMITH 800 1 JAMES 950 1 ADAMS 1100 1 WARD 1250 2 MARTIN 1250 2 MILLER 1300 2 TURNER 1500 3 ALLEN 1600 3 CLARK 2450 4 BLAKE 2850 4 JONES 2975 4 SCOTT 3000 4 FORD 3000 4 KING 5000 5 14 rows selected.
同一张表内的连接查询,即把一个表映射成两个表。主要用于表的自参照关系,比如emp中的上下级或层次关系。因为自连接是同一张表之间的链接查询,所以必须定义表别名。
例子:根据empno和mgr的对应关系,可以查找出KING的下属都有谁
idle> select b.ename||‘‘‘s manager is ‘||a.ename from emp a,emp b where a.empno = b.mgr and a.ename=‘KING‘; B.ENAME||‘‘‘SMANAGERIS‘||A.ENAME ---------------------------------- BLAKE‘s manager is KING JONES‘s manager is KING CLARK‘s manager is KING
不仅返回满足连接的记录,还会返回不满足连接的记录。外链接的运算符(+)该符号可以放在等的左边或右边,但一定要放在缺少信息(完全满足条件的行)的一边。若加在多信息的一边,则没有效果。
例如:emp中不包含40号部门的员工 而dept中有40号部门
idle> select empno,ename,b.deptno,loc from emp a,dept b where a.deptno(+) = b.deptno; EMPNO ENAME DEPTNO LOC ---------- ---------- ---------- ------------- 7782 CLARK 10 NEW YORK 7839 KING 10 NEW YORK 7934 MILLER 10 NEW YORK 7566 JONES 20 DALLAS 7902 FORD 20 DALLAS 7876 ADAMS 20 DALLAS 7369 SMITH 20 DALLAS 7788 SCOTT 20 DALLAS 7521 WARD 30 CHICAGO 7844 TURNER 30 CHICAGO 7499 ALLEN 30 CHICAGO 7900 JAMES 30 CHICAGO 7698 BLAKE 30 CHICAGO 7654 MARTIN 30 CHICAGO 40 BOSTON 15 rows selected.
查询出所有部门名和10号部门内的员工。因为员工信息显示较少 所以(+)符号放在emp.deptno上
idle> select b.deptno,loc,empno,ename from emp a,dept b where a.deptno(+) = b.deptno and a.deptno(+)=10; DEPTNO LOC EMPNO ENAME ---------- ------------- ---------- ---------- 10 NEW YORK 7782 CLARK 10 NEW YORK 7934 MILLER 10 NEW YORK 7839 KING 20 DALLAS 30 CHICAGO 40 BOSTON 6 rows selected.
外连接的注意事项:
语法:
select table1.column_name,table2.column_name from table1
[cross join table2| natural join table2] | [join table2 using (column_name)]
| [join table2 on(table1.column_name = table2.column_name)]
| [left|right|full outer join table2 on(table1.column_name = table2.column_name)];
t1 cross join t2 用于返回笛卡尔集
t1 natural join t2 用于基于同名列执行等连接
t1 join t2 using (column_name) 用于基于特定列执行等连接
t1 join t2 on(table1.column_name = table2.column_name) 用于指定连接条件
t1 left outer join t2 on(col1=col2) 用于左连接
t1 right outer jion t2 on(col1=col2) 用于右连接
t1 full join t2 on(col1=col2) 用于完全连接
select * from emp cross join dept;
等同于select * from emp,dept;
两个表要有共有列,并且数据类型一致。如果两个表的共有列不只一个,natural join会按所有共有列连接。
idle> select * from emp natural join dept;
等同于
idle> select * from emp,dept where emp.deptno=dept.deptno;
oracle的写法会将同名列显示两次,而自然连接则对同名列只显示一次。
注意事项:
select * from emp join dept using (deptno);
等同于
select * from emp,dept where emp.deptno=dept.deptno;
select * from emp join dept on emp.deptno=dept.deptno;
等同于
select * from emp,dept where emp.deptno=dept.deptno;
多条件用and连接或再添加where子句
SQL> select * from emp join dept on emp.deptno=dept.deptno and emp.deptno=10; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO DEPTNO DNAME LOC ---------- ---------- --------- ---------- --------- ---------- ---------- ---------- ---------- -------------- ------------- 7782 CLARK MANAGER 7839 09-JUN-81 2450 10 10 ACCOUNTING NEW YORK 7839 KING PRESIDENT 17-NOV-81 5000 10 10 ACCOUNTING NEW YORK 7934 MILLER CLERK 7782 23-JAN-82 1300 10 10 ACCOUNTING NEW YORK SQL> select * from emp join dept on emp.deptno=dept.deptno where emp.deptno=10 order by 1 desc; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO DEPTNO DNAME LOC ---------- ---------- --------- ---------- --------- ---------- ---------- ---------- ---------- -------------- ------------- 7934 MILLER CLERK 7782 23-JAN-82 1300 10 10 ACCOUNTING NEW YORK 7839 KING PRESIDENT 17-NOV-81 5000 10 10 ACCOUNTING NEW YORK 7782 CLARK MANAGER 7839 09-JUN-81 2450 10 10 ACCOUNTING NEW YORK
满足连接条件的数据,以及不满足连接条件的左边表的其他数据。
select * from dept left outer join emp on emp.deptno=dept.deptno;
等同于
select * from emp,dept where emp.deptno(+) = dept.deptno;
满足连接条件的数据,以及不满足连接条件的右边表的其他数据
select * from emp right outer join dept on emp.deptno=dept.deptno;
等同于
select * from emp,dept where emp.deptno(+) = dept.deptno;
返回连接条件的数据,以及不满足连接条件的左边表和右边表的其他数据。
标签:互斥 特定 结果 意义 sel big img 连接 clerk
原文地址:https://www.cnblogs.com/marxist/p/12111174.html