mysql 链接查询七中情况_sql中的七中join理论

最近有时间回顾了下mysql的join操作,特写个总结,以防自己下次还是遗忘或者是工作要用到时疏忽导致错误,废话不多说,本文将通过具体例子介绍SQL中的各种常用Join的特性和使用场合:

1.测试中使用的mysql,且主要的示例表结构及操作数据如下:

CREATE TABLE `tbl_dept`(

`id` INT(11) NOT NULL AUTO_INCREMENT,

`deptName` VARCHAR(30) DEFAULT NULL,

`locAdd` VARCHAR(40) DEFAULT NULL,

PRIMARY KEY (`id`)

)ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

CREATE TABLE `tbl_emp`(

`id` INT(11) NOT NULL AUTO_INCREMENT,

`name` VARCHAR(30) DEFAULT NULL,

`deptId` INT(11) DEFAULT NULL,

PRIMARY KEY (`id`),

KEY `fk_dept_id` (`deptId`)

#CONSTRAINT `fk_dept_id` FOREIGN KEY (`deptId`) REFERENCES `tbl_dept` (`id`)

)ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

insert into tbl_dept(deptName,locAdd) VALUES('RD',11);

insert into tbl_dept(deptName,locAdd) VALUES('HR',12);

insert into tbl_dept(deptName,locAdd) VALUES('MK',13);

insert into tbl_dept(deptName,locAdd) VALUES('MIS',14);

insert into tbl_dept(deptName,locAdd) VALUES('FD',15);

insert into tbl_emp(name,deptId) VALUES('Z3',1);

insert into tbl_emp(name,deptId) VALUES('Z4',1);

insert into tbl_emp(name,deptId) VALUES('Z5',1);

insert into tbl_emp(name,deptId) VALUES('w5',2);

insert into tbl_emp(name,deptId) VALUES('w6',2);

insert into tbl_emp(name,deptId) VALUES('s7',3);

insert into tbl_emp(name,deptId) VALUES('s8',4);

insert into tbl_emp(name,deptId) VALUES('s9',51);

以下是上述sql数据库中tbl_dept和tbl_emp的数据情况:

e83d3b650636?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

tbl_dept.png

e83d3b650636?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

tbl_emp.png

下面我们通过观察模型,基于上述的表结构语句构造join查询:

内连接

e83d3b650636?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

内连接.png

而实际sql执行情况如下:

e83d3b650636?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

内连接sql.png

左链接

e83d3b650636?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

左链接.png

sql执行情况:

e83d3b650636?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

左链接sql.png

右链接

e83d3b650636?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

右链接.png

sql执行情况:

e83d3b650636?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

右链接sql.png

左外链接

e83d3b650636?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

左外链接.png

sql执行情况:

e83d3b650636?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

左外链接sql.png

右外链接

e83d3b650636?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

右外链接.png

sql执行情况:

e83d3b650636?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

右外链接sql.png

全链接

e83d3b650636?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

全链接.png

但遗憾的是mysql不支持这种固定格式的写法,但oracle支持。但是,我们可以使用union关键字合并且去重实现,具体sql如下:

e83d3b650636?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

全链接sql.png

全外链接

e83d3b650636?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

全外链接.png

mysql同样也不支持这种固定格式的写法,但oracle支持。同样我们只能使用union关键字实现:

e83d3b650636?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

全外链接sql.png

以上内容只是简单展示了两张表之间的主外键关联join,涉及到多表的操作其实是大同小异的,嗯,所以仅提供一个借鉴!工作中还需灵活运用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值