Reviewing the Old Sql Server2008 series 09: inquiry - join query (join key)

Connection, the core query; simply means that multi-table joint inquiry;

Such as: employees table emp, which records all employee information, including employee department number, but no name of the department;

  There are department table dept, which records all sectors of information, including department number, name, number of departments, offices and so on;

Now you want to search the names of all employees and all the department name, only the employee table or query only query the department table are not satisfied;

This time we need to use a multi-table query;

As there are two simple query query:

. 1  - Query Five record 
2  SELECT  Top  . 5 emp.name, dept.name 
 . 3      from EMP, Dept 
 . 4      WHERE emp.deptid = dept.deptid 
 . 5      Order  by emp.name
 . 6  SELECT  Top  . 5 emp.name, dept.name 
 . 7      from EMP
 . 8      Inner  the Join Dept ON emp.deptid = dept.deptid      - connection by: some redundant data does not meet the criteria to filter out (connected Cartesian product) 
. 9      Order  by emp.name

 

This is the basic inner join: inner join, but inner usually omitted, written directly join; 

When the query has duplicate field names, need to distinguish table_name.field_name; can also be used instead of the alias name to simplify the writing table.

The above query can also be written directly:

select top 5 a.name,b.name from emp a,dept b where a.deptid=b.deptid order by a.name

select top 5 a.name,b.name from emp a join dept b on a.deptid = b.deptid order by a.name

The results are consistent;

 

 Of course, an inner connector, the external connection naturally; left connecting left outer join, right connecting right outer join; fully-connected full outer join, outer generally omitted;

But they are similar, while the connector is the most frequently used;

Attached several other connections:

1, left join: the left connecting -> tableA left join tableB [on tableA.field = tableB.field]: The resulting query log, if not in conformity with the left table (ON condition),

  Also enumerated, tableB no data is displayed to NULL

2, right join: the right connection to the left join just the opposite

3, full join (Cartesian product) displays all records about the two tables, all the display does not match the criteria of NULL, with very little of this, almost do not use the

 

Guess you like

Origin www.cnblogs.com/azrealer/p/11897346.html