create table a (id number(3),name varchAR2(20));
create table b (id number(3),name varchar2(50));
insert into a values(1,'小一');
insert into a values(2,'小二');
insert into a values(3,'小三');
insert into b values(1,'小小');
insert into b values(2,'小中');
insert into b values(4,'小大');
insert into b values(5,'大大');
--连接分为两类:1、内连接 2、外连接(左外连接、右外连接、全外连接(只要有数据,不匹配的表字段补null))3、笛卡尔集(交叉连接)
--以下第一条为数据库中通用写法,第二条为Oracle数据库特有:
--内连接:(对于不匹配的都会进行舍弃)
select * from a inner join b on a.id=b.id;
select * from a,b where a.id = b.id;
--外连接:(对于不匹配的字段补null)
--左外连接:(以左表为基表(驱动表),将左表的每一条数据都与右表匹配,如果在右表中没有匹配数据,则右表补null)
select * from a left join b on a.id=b.id;
select * from a,b where a.id=b.id(+);
--右外连接:(以右表为基表(驱动表),将右表的每一条数据都与左表匹配,如果在左表中没有匹配数据,则左表补null)
select * from a right join b on a.id = b.id;
select * from a,b where a.id(+) = b.id;
--全外连接:左表和右表没有符合条件的都补null值
select * from a full join b on a.id = b.id;
select * from a,b where a.id(+)=b.id union select * from a,b where a.id = b.id(+);
--交叉连接:(全连接,笛卡尔集,全排列)
select * from a cross join b;
create table b (id number(3),name varchar2(50));
insert into a values(1,'小一');
insert into a values(2,'小二');
insert into a values(3,'小三');
insert into b values(1,'小小');
insert into b values(2,'小中');
insert into b values(4,'小大');
insert into b values(5,'大大');
--连接分为两类:1、内连接 2、外连接(左外连接、右外连接、全外连接(只要有数据,不匹配的表字段补null))3、笛卡尔集(交叉连接)
--以下第一条为数据库中通用写法,第二条为Oracle数据库特有:
--内连接:(对于不匹配的都会进行舍弃)
select * from a inner join b on a.id=b.id;
select * from a,b where a.id = b.id;
--外连接:(对于不匹配的字段补null)
--左外连接:(以左表为基表(驱动表),将左表的每一条数据都与右表匹配,如果在右表中没有匹配数据,则右表补null)
select * from a left join b on a.id=b.id;
select * from a,b where a.id=b.id(+);
--右外连接:(以右表为基表(驱动表),将右表的每一条数据都与左表匹配,如果在左表中没有匹配数据,则左表补null)
select * from a right join b on a.id = b.id;
select * from a,b where a.id(+) = b.id;
--全外连接:左表和右表没有符合条件的都补null值
select * from a full join b on a.id = b.id;
select * from a,b where a.id(+)=b.id union select * from a,b where a.id = b.id(+);
--交叉连接:(全连接,笛卡尔集,全排列)
select * from a cross join b;
select * from a,b;