【数据库】内连接、左连接、右连接、全连接、自连接


drop database IF EXISTS sqljiontest;
create database sqljiontest;
	use sqljiontest;

drop table IF EXISTS book;
create table book(
	id int(11) NOT NULL AUTO_INCREMENT,
	stuid int(11),
	bookname varchar(50) COLLATE utf8_bin,
	PRIMARY KEY(id)
)ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

insert into book(id,stuid,bookname)  values(1,1,'python');
insert into book(id,stuid,bookname)  values(2,2,'java');
insert into book(id,stuid,bookname)  values(3,2,'java');
insert into book(id,stuid,bookname)  values(4,4,'php');
insert into book(id,stuid,bookname)  values(5,5,'c');
/*insert into book(id,stuid,bookname)  values(5,6,'c22');  提示主键重复*/
insert into book(id,stuid,bookname)  values(6,7,'fortran');
insert into book(id,stuid,bookname)  values(7,15,NULL);

drop table IF EXISTS stu;
create table stu(
	stuid int(11) NOT NULL AUTO_INCREMENT,
	stuname varchar(50),
	PRIMARY KEY(stuid)
)ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

insert into stu(stuid,stuname)  values(1,'小明');
insert into stu(stuid,stuname)  values(2,'小红');
insert into stu(stuid,stuname)  values(3,'小啥');
insert into stu(stuid,stuname)  values(4,'小我');
insert into stu(stuid,stuname)  values(5,'小好');
insert into stu(stuid,stuname)  values(6,'小解');


/*
学习资料:
http://www.cnblogs.com/zxlovenet/p/4005256.html
http://blog.csdn.net/tianlesoftware/article/details/5795896
http://www.cnblogs.com/eflylab/archive/2007/06/25/794278.html
http://www.cnblogs.com/Hadley-pu/p/sql_selfconnect.html   自连接
*/

/*
在写具体连接代码之前一定要注意是mysql还是oracle等具体某种数据库支持语法

Oracle表之间的连接分为三种:
1. 内连接---等值、不等值、自然连接
2. 外连接
  1)左外连接 (左边的表不加限制)
  2)右外连接(右边的表不加限制)
  3)全外连接(左右两表都不加限制)
3.自连接   

SQL的标准语法:
select table1.column,table2.column
from table1 [inner | left | right | full ] join table2 on table1.column1 = table2.column2;
inner join 表示内连接;
left join表示左外连接;
right join表示右外连接;
full join表示完全外连接;
on子句 用于指定连接条件。

注意:
自连接(self join)是SQL语句中经常要用的连接方式,使用自连接可以将自身表的一个镜像当作另一个表来对待,从而能够得到一些特殊的数据。
join 是inner join  因此有时候不需要写inner
如果使用from子句指定内、外连接,则必须要使用on子句指定连接条件;
如果使用(+)操作符指定外连接,则必须使用where子句指定连接条件;
在mysql中使用union代替full join

等值连接和自然连接的区别和联系 
1、自然连接一定是等值连接,但等值连接不一定是自然连接。等值连接不把重复的属性除去;而自然连接要把重复的属性除去。  
2、等值连接要求相等的分量,不一定是公共属性;而自然连接要求相等的分量必须是公共属性。  
3、等值连接不把重复的属性除去;而自然连接要把重复的属性除去。

用(+)来实现, 这个+号可以这样来理解: + 表示补充,即哪个表有加号,这个表就是匹配表。所以加号写在左表,右表就是全部显示,故是右连接。
 Select * from dave a,bl b where a.id(+)=b.id;
 mysql不支持(+)
*/

/*下面两条语句是等价的--内连接*/
select * from book as a,stu as b where a.stuid=b.stuid; /*多表查询*/
select * from book as a inner join stu as b on a.stuid=b.stuid;
select * from book as a join stu as b on a.stuid=b.stuid;

/*下面两条语句是等价的--自然连接*/
select * from book as a NATURAL JOIN stu as b;
select * from book as a inner join stu as b using(stuid);

/*左连接*/
select * from book as a left join stu as b on a.stuid=b.stuid;

/*右连接*/
select * from book as a right join stu as b on a.stuid=b.stuid;

/*全连接--mysql通过*/
select * from book as a left join stu as b on a.stuid=b.stuid union select * from book as a right join stu as b on a.stuid=b.stuid;

/*全连接---这种写法在mysql会报错*/
select * from book as a full outer join stu as b on a.stuid=b.stuid;

/*交叉连接*/
select * from book as a cross join stu as b order by a.id;

连接


内连接

自然连接

左连接

右连接

全连接

交叉连接



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值