本文主要是介绍多表查询方式,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
笛卡尔积
笛卡尔(Descartes)乘积又叫直积。
假设有两个集合X和Y,其笛卡尔积表示为X*Y,其积表示集合X中的每一个
元素和集合Y中每一个元素的所有可能的有序对
X*Y={(x,y)|x∈X∧y∈Y}
例如:X={a,b},Y={c,d,e},则X*Y={(a,c),(a,d),(a,e),(b,c), (b,d),(b,e)}
多表查询
当需要从多张表中获取数据(即多表联合查询)时,在不加任何限定条件的
情况下就会产生笛卡尔积,下面使用scott的用户下的emp表和dept表
进行联合查询:
SQL> select e.empno,e.ename,e.job,d.deptno,d.dname from emp e,dept d ;EMPNO ENAME JOB DEPTNO DNAME
----- ---------- --------- ------ --------------7369 SMITH CLERK 10 ACCOUNTING7499 ALLEN SALESMAN 10 ACCOUNTING7521 WARD SALESMAN 10 ACCOUNTING7566 JONES MANAGER 10 ACCOUNTING7654 MARTIN SALESMAN 10 ACCOUNTING7698 BLAKE MANAGER 10 ACCOUNTING7782 CLARK MANAGER 10 ACCOUNTING7788 SCOTT ANALYST 10 ACCOUNTING7839 KING PRESIDENT 10 ACCOUNTING7844 TURNER SALESMAN 10 ACCOUNTING7876 ADAMS CLERK 10 ACCOUNTING7900 JAMES CLERK 10 ACCOUNTING7902 FORD ANALYST 10 ACCOUNTING7934 MILLER CLERK 10 ACCOUNTING7369 SMITH CLERK 20 RESEARCH7499 ALLEN SALESMAN 20 RESEARCH7521 WARD SALESMAN 20 RESEARCH7566 JONES MANAGER 20 RESEARCH7654 MARTIN SALESMAN 20 RESEARCH7698 BLAKE MANAGER 20 RESEARCHEMPNO ENAME JOB DEPTNO DNAME
----- ---------- --------- ------ --------------7782 CLARK MANAGER 20 RESEARCH7788 SCOTT ANALYST 20 RESEARCH7839 KING PRESIDENT 20 RESEARCH7844 TURNER SALESMAN 20 RESEARCH7876 ADAMS CLERK 20 RESEARCH7900 JAMES CLERK 20 RESEARCH7902 FORD ANALYST 20 RESEARCH7934 MILLER CLERK 20 RESEARCH7369 SMITH CLERK 30 SALES7499 ALLEN SALESMAN 30 SALES7521 WARD SALESMAN 30 SALES7566 JONES MANAGER 30 SALES7654 MARTIN SALESMAN 30 SALES7698 BLAKE MANAGER 30 SALES7782 CLARK MANAGER 30 SALES7788 SCOTT ANALYST 30 SALES7839 KING PRESIDENT 30 SALES7844 TURNER SALESMAN 30 SALES7876 ADAMS CLERK 30 SALES7900 JAMES CLERK 30 SALES7902 FORD ANALYST 30 SALESEMPNO ENAME JOB DEPTNO DNAME
----- ---------- --------- ------ --------------7934 MILLER CLERK 30 SALES7369 SMITH CLERK 40 OPERATIONS7499 ALLEN SALESMAN 40 OPERATIONS7521 WARD SALESMAN 40 OPERATIONS7566 JONES MANAGER 40 OPERATIONS7654 MARTIN SALESMAN 40 OPERATIONS7698 BLAKE MANAGER 40 OPERATIONS7782 CLARK MANAGER 40 OPERATIONS7788 SCOTT ANALYST 40 OPERATIONS7839 KING PRESIDENT 40 OPERATIONS7844 TURNER SALESMAN 40 OPERATIONS7876 ADAMS CLERK 40 OPERATIONS7900 JAMES CLERK 40 OPERATIONS7902 FORD ANALYST 40 OPERATIONS7934 MILLER CLERK 40 OPERATIONS56 rows selected
上面这个查询产生笛卡儿积。
从查询结果中可以发现有许多重复项,这就是产生了笛卡尔积,可以通过在
WHERE子句中增加合适的连接条件来消除笛卡尔积,以下几种方式消除笛卡尔积。
1、Equijoin:等值连接
2、Non-equijoin:不等值连接
3、Outer join:外连接
4、Self join:自连接
1.等值连接
SQL> select e.empno,e.ename,e.job,d.deptno,d.dname from emp e,dept d where e.deptno=d.deptno;EMPNO ENAME JOB DEPTNO DNAME
----- ---------- --------- ------ --------------7782 CLARK MANAGER 10 ACCOUNTING7839 KING PRESIDENT 10 ACCOUNTING7934 MILLER CLERK 10 ACCOUNTING7566 JONES MANAGER 20 RESEARCH7902 FORD ANALYST 20 RESEARCH7876 ADAMS CLERK 20 RESEARCH7369 SMITH CLERK 20 RESEARCH7788 SCOTT ANALYST 20 RESEARCH7521 WARD SALESMAN 30 SALES7844 TURNER SALESMAN 30 SALES7499 ALLEN SALESMAN 30 SALES7900 JAMES CLERK 30 SALES7698 BLAKE MANAGER 30 SALES7654 MARTIN SALESMAN 30 SALES14 rows selected
2.不等值连接
SQL> select e.empno, e.ename, e.sal, s.grade2 from emp e, SALGRADE s3 where e.sal >= s.losal and e.sal <= s.hisal4 ;EMPNO ENAME SAL GRADE
----- ---------- --------- ----------7369 SMITH 800.00 17900 JAMES 1045.00 17876 ADAMS 1100.00 17521 WARD 1250.00 27654 MARTIN 1250.00 27934 MILLER 1300.00 27844 TURNER 1500.00 37499 ALLEN 1600.00 37782 CLARK 2450.00 47698 BLAKE 2850.00 47566 JONES 2975.00 47788 SCOTT 3000.00 47902 FORD 3000.00 47839 KING 5000.00 514 rows selected
3.外连接
外连接分为三种类型。
1)左外连接
方式一:使用left outer join
SQL> select e.empno,e.ename,e.job,e.sal,e.deptno,d.dname,d.loc,d.deptno2 from emp e left outer join dept d3 on e.deptno=d.deptno4 ;EMPNO ENAME JOB SAL DEPTNO DNAME LOC DEPTNO
----- ---------- --------- --------- ------ -------------- ------------- ------7934 MILLER CLERK 1300.00 10 ACCOUNTING NEW YORK 107839 KING PRESIDENT 5000.00 10 ACCOUNTING NEW YORK 107782 CLARK MANAGER 2450.00 10 ACCOUNTING NEW YORK 107902 FORD ANALYST 3000.00 20 RESEARCH DALLAS 207876 ADAMS CLERK 1100.00 20 RESEARCH DALLAS 207788 SCOTT ANALYST 3000.00 20 RESEARCH DALLAS 207566 JONES MANAGER 2975.00 20 RESEARCH DALLAS 207369 SMITH CLERK 800.00 20 RESEARCH DALLAS 207900 JAMES CLERK 1045.00 30 SALES CHICAGO 307844 TURNER SALESMAN 1500.00 30 SALES CHICAGO 307698 BLAKE MANAGER 2850.00 30 SALES CHICAGO 307654 MARTIN SALESMAN 1250.00 30 SALES CHICAGO 307521 WARD SALESMAN 1250.00 30 SALES CHICAGO 307499 ALLEN SALESMAN 1600.00 30 SALES CHICAGO 3014 rows selected
方式二: 用外连接符(+)进行连接
SQL> select e.empno,e.ename,e.job,e.sal,e.deptno,d.dname,d.loc,d.deptno2 from emp e,dept d3 where e.deptno=d.deptno(+)4 ;EMPNO ENAME JOB SAL DEPTNO DNAME LOC DEPTNO
----- ---------- --------- --------- ------ -------------- ------------- ------7934 MILLER CLERK 1300.00 10 ACCOUNTING NEW YORK 107839 KING PRESIDENT 5000.00 10 ACCOUNTING NEW YORK 107782 CLARK MANAGER 2450.00 10 ACCOUNTING NEW YORK 107902 FORD ANALYST 3000.00 20 RESEARCH DALLAS 207876 ADAMS CLERK 1100.00 20 RESEARCH DALLAS 207788 SCOTT ANALYST 3000.00 20 RESEARCH DALLAS 207566 JONES MANAGER 2975.00 20 RESEARCH DALLAS 207369 SMITH CLERK 800.00 20 RESEARCH DALLAS 207900 JAMES CLERK 1045.00 30 SALES CHICAGO 307844 TURNER SALESMAN 1500.00 30 SALES CHICAGO 307698 BLAKE MANAGER 2850.00 30 SALES CHICAGO 307654 MARTIN SALESMAN 1250.00 30 SALES CHICAGO 307521 WARD SALESMAN 1250.00 30 SALES CHICAGO 307499 ALLEN SALESMAN 1600.00 30 SALES CHICAGO 3014 rows selected
2)右外连接
右外连接同理
3)全外连接
连接A和B两张表,返回A表和B表中所有的数据进 行的连接结果,可以在FROM语句中使用全外连接语法 FULL [OUTER] JOIN对两张表进行连接
SQL> select e.empno,e.ename,e.job,e.sal,e.deptno,d.dname,d.loc,d.deptno2 from emp e3 full outer join4 dept d5 on e.deptno=d.deptno;EMPNO ENAME JOB SAL DEPTNO DNAME LOC DEPTNO
----- ---------- --------- --------- ------ -------------- ------------- ------7369 SMITH CLERK 800.00 20 RESEARCH DALLAS 207499 ALLEN SALESMAN 1600.00 30 SALES CHICAGO 307521 WARD SALESMAN 1250.00 30 SALES CHICAGO 307566 JONES MANAGER 2975.00 20 RESEARCH DALLAS 207654 MARTIN SALESMAN 1250.00 30 SALES CHICAGO 307698 BLAKE MANAGER 2850.00 30 SALES CHICAGO 307782 CLARK MANAGER 2450.00 10 ACCOUNTING NEW YORK 107788 SCOTT ANALYST 3000.00 20 RESEARCH DALLAS 207839 KING PRESIDENT 5000.00 10 ACCOUNTING NEW YORK 107844 TURNER SALESMAN 1500.00 30 SALES CHICAGO 307876 ADAMS CLERK 1100.00 20 RESEARCH DALLAS 207900 JAMES CLERK 1045.00 30 SALES CHICAGO 307902 FORD ANALYST 3000.00 20 RESEARCH DALLAS 207934 MILLER CLERK 1300.00 10 ACCOUNTING NEW YORK 10OPERATIONS BOSTON 4015 rows selected
4.自连接
表A和自己进行连接操作,此时要为表起个别名,通过为表起别名可将同一张表视为多张表
SQL> select e1.ename || '的老板是: ' || e2.ename2 from emp e1,emp e23 where e1.mgr=e2.empno;E1.ENAME||'的老板是:'||E2.ENAM
------------------------------
FORD的老板是: JONES
SCOTT的老板是: JONES
TURNER的老板是: BLAKE
ALLEN的老板是: BLAKE
WARD的老板是: BLAKE
JAMES的老板是: BLAKE
MARTIN的老板是: BLAKE
MILLER的老板是: CLARK
ADAMS的老板是: SCOTT
BLAKE的老板是: KING
JONES的老板是: KING
CLARK的老板是: KING
SMITH的老板是: FORD13 rows selected
这篇关于多表查询方式的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!