MySql JOIN 七种连接方式

MySql JOIN 七种连接方式

 

 

 MySql 中 有其中连接方式:

Sample:

#创建两张表
CREATE TABLE `tbl_emp` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
`deptId` int(11) DEFAULT NULL,
PRIMARY KEY (`id`) ,
KEY `fk_dept_id`(`deptId`)
)ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8;

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 CHARACTER SET = utf8;

#插入数据
INSERT INTO `tbl_emp` VALUES (1,‘z3‘,1),(2,‘z4‘,1),(3,‘z5‘,1),(4,‘w5‘,2),(5,‘w6‘,2),(6,‘s7‘,3),(7,‘s8‘,4),(8,‘s9‘,51);
INSERT INTO `tbl_dept` VALUES (1,‘RD‘,‘11‘),(2,‘HR‘,‘12‘),(3,‘MK‘,‘13‘),(4,‘MIS‘,‘14‘),(5,‘FD‘,‘15‘);

#未执行连接查询时,查出来的是笛卡尔积 select count(*) from tbl_emp e,tbl_dept d; #执行连接查询 #$1.第一张图:执行左连接查询 并且只要A的全部,不要B独有的部分 select * from tbl_emp e left join tbl_dept d on e.deptId=d.id; #$2.第二张图:执行内连接查询 并且只要AB共有的部分 select * from tbl_emp e inner join tbl_dept d on e.deptId=d.id; #$3.第三张图:执行右连接查询 并且只要B的全部,不要A独有的部分 select * from tbl_emp e right join tbl_dept d on e.deptId=d.id; #$4.第四张图:执行左连接查询 并且只要A独有的部分(加入where) select * from tbl_emp e left join tbl_dept d on e.deptId=d.id where d.id is null; #$5.第五张图:执行左连接查询 并且只要B独有的部分(加入where) select * from tbl_emp e right join tbl_dept d on e.deptId=d.id where e.id is null; #$6.第六张图:执行外连接查询 并且AB全要(加入union过滤) select * from tbl_emp e left join tbl_dept d on e.deptId=d.id -> union -> select * from tbl_emp e right join tbl_dept d on e.deptId=d.id; #$7.第七张图:执行外连接查询 并且只要A独有的部分和B独有的部分(加入union过滤) select * from tbl_emp e left join tbl_dept d on e.deptId=d.id where d.id is null -> union -> select * from tbl_emp e right join tbl_dept d on e.deptId=d.id where e.deptId is null;

  

 

MySql JOIN 七种连接方式

上一篇:k8s 部署项目


下一篇:kubernetes常用命令