您的位置:首页 > 数据库

数据库中表的连接方式详解

2016-11-16 13:27 218 查看


数据库中表的连接方式详解

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;

select * from a,b;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: