【数据库】内连接、左连接、右连接、全连接、自连接
来源:互联网 发布:回到2005年txt下载知轩 编辑:程序博客网 时间:2024/05/23 19:20
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.htmlhttp://blog.csdn.net/tianlesoftware/article/details/5795896http://www.cnblogs.com/eflylab/archive/2007/06/25/794278.htmlhttp://www.cnblogs.com/Hadley-pu/p/sql_selfconnect.html 自连接*//*在写具体连接代码之前一定要注意是mysql还是oracle等具体某种数据库支持语法Oracle表之间的连接分为三种:1. 内连接---等值、不等值、自然连接2. 外连接 1)左外连接 (左边的表不加限制) 2)右外连接(右边的表不加限制) 3)全外连接(左右两表都不加限制)3.自连接 SQL的标准语法:select table1.column,table2.columnfrom 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;
连接
内连接
自然连接
左连接
右连接
全连接
交叉连接
0 0
- 内连接,外连接,左连接,右连接,全连接,内连接,交叉连接,自连接
- 内连接,外连接,左连接,右连接,全连接,内连接,交叉连接,自连接
- 【数据库】内连接、左连接、右连接、全连接、自连接
- SQL 连接 左连接,右连接,全连接,内连接,交叉连接,自连接
- 内连接、外连接[左连接、右连接、全连接]、交叉连接、自连接
- SQL 连接 JOIN 例解。(左连接,右连接,全连接,内连接,交叉连接,自连接
- SQL 连接 JOIN 例解。(左连接,右连接,全连接,内连接,交叉连接,自连接)
- 数据库中查询的各种连接(左连接,右连接,全连接,内连接,交叉连接,自连接)
- 数据库中查询的各种连接(左连接,右连接,全连接,内连接,交叉连接,自连接)
- 左连接,右连接,全连接,内连接,交叉连接,自连接
- 内连接&左连接&右连接&全连接&交叉连接&自连接
- SQL各种连接(左连接、右连接、内连接、自连接、全连接)
- 内连接,外连接,左连接,右连接,全连接
- mysql连接内连接、左连接、右连接、全连接
- mysql连接内连接、左连接、右连接、全连接
- 数据库:左连接 、右连接 、全连接
- [转]SQl中的左连接,右连接,内连接,全连接,自连接的理解
- SQl中的左连接,右连接,内连接,全连接,自连接的理解。
- SRM548 Div1Medium KingdomAndDice
- 过滤器和监听器
- nyoj541 最强DE 战斗力 (大数问题)
- linux osx 查看文件编码
- atexit函数
- 【数据库】内连接、左连接、右连接、全连接、自连接
- 如何更改linux文件的拥有者及用户组(chown和chgrp)
- python2.7使用pip安装模块报错Unable to find vcvarsall.bat
- 使用ImageLoader实现图片异步加载
- ios命令行自助打包-解析描述文件,获取uuid,bundle identifier,开发者团队
- Boot常用入门库分析
- BOVW基本思想
- poj 2135 最小费用流
- Java中的垃圾回收概述及注意点