--09 SQL statement, multi-table queries

Peer connections

By two table columns have the same meaning, the connection is established conditions. Results show only two columns is the value of the line data equivalent conditions. You must add the table name prefix modified when the table is selected with the same name, otherwise it is impossible to determine which table this column belongs.

Press equivalence relation deptno combined two tables of all the columns

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.

When the two tables have the same column name, in order to distinguish, to add a prefix in front of the table column masterpiece. When not in conflict, can be used directly

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

Ranging connections

Is exactly equal to exclude other conditions>, <,! =, <=,> =, Between and. Mainly specific range of the display information between different tables (relations may be understood to include)

For example: SALGRADE table wages divided five grades
idle> select * from SALGRADE;

GRADE LOSAL HISAL
---------- ---------- ----------
1 700 1200
2 1201 1400
3 1401 2000
4 2001 3000
5 3001 9999

idle>

Investigate each employee's pay grade
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.

Since the connection

With a join query in a table, a table that is mapped to two tables. Referring mainly used for the table from the relationship, such as a lower hierarchy or the emp. Because the self-join is a link between the query with a table, you must define the alias table.

Example: According to correspondence between the empno and mgr, you can find out who has a subsidiary 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

External link

Not only return records that meet the connection, but also return records do not satisfy the connection. Linked operator (+) sign can be placed on the left or right or the like, but it must be a lack of information on the side (completely satisfy the condition lines). If added to the side of the multi-information, there is no effect.

For example: emp does not include the number of employees and 40 departments, 40 departments have dept

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.

Check out the names of employees in all departments and 10 departments. Because the employee information show less so (+) sign on 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.

Notes outer join:

  1. Each condition needs to be used where there are a plurality of conditions (+) sign
  2. (+) Only in the column can not be an expression
  3. (+) Does not operate in or with

SQL 1999 query methods

grammar:
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  the Join T2 set for returning the Cartesian
Natural T1 the Join T2 based on the connection with the execution ranking
T1 the Join T2 the using (column_name) for execution on a particular column is connected
T1 the Join T2 ON (table1.column_name = table2.column_name) specifies the join condition
T1 left  Outer  the Join T2 ON (col1 = col2) is connected to the left
T1 right  Outer jion T2 ON (col1 = col2) is connected to the right
T1 Full  the Join T2 ON (col1 = col2) for fully connected

cross join

select * from emp cross join dept;

 

Equivalent to select * from emp, dept;

natural join

There are two tables have the column, and the same data type. If there are more than one column of two tables, natural join will be connected by a total of all the columns.

idle> select * from emp natural join dept;

Equivalent to 

idle> select * from emp,dept where emp.deptno=dept.deptno;

 

oracle wording will be displayed twice with the same name, is naturally connected to the same ranked displayed only once.

USING clause

Precautions:

  1. and using natural join clauses are mutually exclusive and can not be used simultaneously
  2. You can not use columns referenced table name prefix
  3. Referenced column must be a total of two columns of the table
  4. Multi-column are the same only choose one

select * from emp join dept using (deptno);

Equivalent to

 

select * from emp,dept where emp.deptno=dept.deptno;

on clause

select * from emp join dept on emp.deptno=dept.deptno;

Equivalent to

select * from emp,dept where emp.deptno=dept.deptno;

And connecting multiple conditions or add a where clause

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

Left outer join

Satisfy the join condition data, and other data does not satisfy the condition table on the left of the connector.

select * from dept left outer join emp on emp.deptno=dept.deptno;

Equivalent to

 

select * from emp,dept where emp.deptno(+) = dept.deptno;

Right outside link

 

Satisfy the join condition data, and other data to the right of the table does not meet the join condition

select * from emp right outer join dept on emp.deptno=dept.deptno;

Equivalent to

 

select * from emp,dept where emp.deptno(+) = dept.deptno;

Fully connected

 

Returns the connection condition data, and other data does not meet the table on the left and right table join condition.

 

Guess you like

Origin www.cnblogs.com/marxist/p/12111174.html