下面代码是在Microsoft SQL Server 2000 里运行
-- 关于sql语句中的连接(join)关键字,是较为常用而又不太容易理解的关键字,下面这个例子给出了一个简单的解释(以下均在查询分析器中执行)
-- 删除已存在的表和数据库
use master
if exists(select * from sysdatabases where name='Study_Join')
begin
if exists(select count(*) from sysobjects where id = object_id('Study_Join.dbo.table1'))
begin
drop table Study_Join.dbo.table1
end
if exists(select count(*) from sysobjects where id = object_id('Study_Join.dbo.table2'))
begin
drop table Study_Join.dbo.table2
end
drop database Study_Join;
end
-- 建数据库Study_Join:
create database Study_Join;
go
use Study_Join;
-- 建表table1,table2:
create table table1(id int,name varchar(10));
insert into table1 values(1,'lee');
insert into table1 values(2,'zhang');
insert into table1 values(4,'wang');
create table table2(id int,score int);
insert into table2 values(1,90);
insert into table2 values(2,100);
insert into table2 values(3,70);
/*
-- Oracle不支持此写法,MSSQL与MySQL支持
create table table1(id int,name varchar(10));
insert into table1 select 1,'lee';
insert into table1 select 2,'zhang';
insert into table1 select 4,'wang';
create table table2(id int,score int);
insert into table2 select 1,90;
insert into table2 select 2,100;
insert into table2 select 3,70;
*/
/*
结果如下表所示:
table1 table2
-------------------------------
id name | id score |
-------------------------------
1 lee | 1 90 |
2 zhang | 2 100 |
4 wang | 3 70 |
-------------------------------
*/
-- ****************************************************************************
-- 一、外连接
-- 左(外)连接(left join 等价于 left outer join)
select * from table1 left outer join table2 on table1.id=table2.id;
/*
注释:包含table1的所有子句,根据指定条件返回table2相应的字段,不符合的以null显示
------------------------------
id name id score
------------------------------
1 lee 1 90
2 zhang 2 100
4 wang null null
------------------------------
*/
-- 右(外)连接(right join 等价于 right outer join)
select * from table1 right outer join table2 on table1.id=table2.id;
/*
注释:包含table2的所有子句,根据指定条件返回table1相应的字段,不符合的以null显示
------------------------------
id name id score
------------------------------
1 lee 1 90
2 zhang 2 100
null null 3 70
------------------------------
*/
-- 全(外)连接(full join 等价于 full outer join)
-- MySQL不支持此写法,MSSQL,Oracle支持
select * from table1 full outer join table2 on table1.id=table2.id;
-- MSSQL,MySQL,Oracle都支持
select * from table1 left outer join table2 on table1.id=table2.id
union
select * from table1 right outer join table2 on table1.id=table2.id;
/*
-- 三个表
select * from table1 left outer join table2 on table1.id = table2.id left outer join table3 on table2.id = table3.id
union
select * from table1 right outer join table2 on table1.id = table2.id left outer join table3 on table2.id = table3.id
union
select * from table1 right outer join table2 on table1.id = table2.id right outer join table3 on table2.id = table3.id
*/
/*
注释:返回左右连接的并集(见上左、右连接)
------------------------------
id name id score
------------------------------
1 lee 1 90
2 zhang 2 100
4 wang null null
null null 3 70
------------------------------
*/
-- ****************************************************************************
-- 二、内连接
-- 内连接(join 等价于 inner join)
select * from table1 inner join table2 on table1.id=table2.id;
/*
注释:返回左右连接的交集(见上左、右连接)
------------------------------
id name id score
------------------------------
1 lee 1 90
2 zhang 2 100
------------------------------
等价(与下列执行效果相同)
select a.*,b.* from table1 a,table2 b where a.id=b.id;
select * from table1 cross join table2 where table1.id=table2.id; -- 注:cross join后加条件只能用where,不能用on
*/
-- ****************************************************************************
-- 三、交叉连接(完全)
select * from table1 cross join table2;
/*
注释:返回3*3=9条记录,即笛卡尔积
------------------------------
id name id score
------------------------------
1 lee 1 90
2 zhang 1 90
4 wang 1 90
1 lee 2 100
2 zhang 2 100
4 wang 2 100
1 lee 3 70
2 zhang 3 70
4 wang 3 70
------------------------------
*/
-- 删除已存在的表和数据库
use master
if exists(select * from sysdatabases where name='Study_Join')
begin
if exists(select count(*) from sysobjects where id = object_id('Study_Join.dbo.table1'))
begin
drop table Study_Join.dbo.table1
end
if exists(select count(*) from sysobjects where id = object_id('Study_Join.dbo.table2'))
begin
drop table Study_Join.dbo.table2
end
drop database Study_Join;
end
-- 建数据库Study_Join:
create database Study_Join;
go
use Study_Join;
-- 建表table1,table2:
create table table1(id int,name varchar(10));
insert into table1 values(1,'lee');
insert into table1 values(2,'zhang');
insert into table1 values(4,'wang');
create table table2(id int,score int);
insert into table2 values(1,90);
insert into table2 values(2,100);
insert into table2 values(3,70);
/*
-- Oracle不支持此写法,MSSQL与MySQL支持
create table table1(id int,name varchar(10));
insert into table1 select 1,'lee';
insert into table1 select 2,'zhang';
insert into table1 select 4,'wang';
create table table2(id int,score int);
insert into table2 select 1,90;
insert into table2 select 2,100;
insert into table2 select 3,70;
*/
/*
结果如下表所示:
table1 table2
-------------------------------
id name | id score |
-------------------------------
1 lee | 1 90 |
2 zhang | 2 100 |
4 wang | 3 70 |
-------------------------------
*/
-- ****************************************************************************
-- 一、外连接
-- 左(外)连接(left join 等价于 left outer join)
select * from table1 left outer join table2 on table1.id=table2.id;
/*
注释:包含table1的所有子句,根据指定条件返回table2相应的字段,不符合的以null显示
------------------------------
id name id score
------------------------------
1 lee 1 90
2 zhang 2 100
4 wang null null
------------------------------
*/
-- 右(外)连接(right join 等价于 right outer join)
select * from table1 right outer join table2 on table1.id=table2.id;
/*
注释:包含table2的所有子句,根据指定条件返回table1相应的字段,不符合的以null显示
------------------------------
id name id score
------------------------------
1 lee 1 90
2 zhang 2 100
null null 3 70
------------------------------
*/
-- 全(外)连接(full join 等价于 full outer join)
-- MySQL不支持此写法,MSSQL,Oracle支持
select * from table1 full outer join table2 on table1.id=table2.id;
-- MSSQL,MySQL,Oracle都支持
select * from table1 left outer join table2 on table1.id=table2.id
union
select * from table1 right outer join table2 on table1.id=table2.id;
/*
-- 三个表
select * from table1 left outer join table2 on table1.id = table2.id left outer join table3 on table2.id = table3.id
union
select * from table1 right outer join table2 on table1.id = table2.id left outer join table3 on table2.id = table3.id
union
select * from table1 right outer join table2 on table1.id = table2.id right outer join table3 on table2.id = table3.id
*/
/*
注释:返回左右连接的并集(见上左、右连接)
------------------------------
id name id score
------------------------------
1 lee 1 90
2 zhang 2 100
4 wang null null
null null 3 70
------------------------------
*/
-- ****************************************************************************
-- 二、内连接
-- 内连接(join 等价于 inner join)
select * from table1 inner join table2 on table1.id=table2.id;
/*
注释:返回左右连接的交集(见上左、右连接)
------------------------------
id name id score
------------------------------
1 lee 1 90
2 zhang 2 100
------------------------------
等价(与下列执行效果相同)
select a.*,b.* from table1 a,table2 b where a.id=b.id;
select * from table1 cross join table2 where table1.id=table2.id; -- 注:cross join后加条件只能用where,不能用on
*/
-- ****************************************************************************
-- 三、交叉连接(完全)
select * from table1 cross join table2;
/*
注释:返回3*3=9条记录,即笛卡尔积
------------------------------
id name id score
------------------------------
1 lee 1 90
2 zhang 1 90
4 wang 1 90
1 lee 2 100
2 zhang 2 100
4 wang 2 100
1 lee 3 70
2 zhang 3 70
4 wang 3 70
------------------------------
*/