做者:haifeisisql
文章出處: MySQL 的七種 join數據庫
建立數據庫表'tbl_dept'和'tbl_emp'ide
CREATE DATABASE db0206; USE db0206; CREATE TABLE `db0206`.`tbl_dept`( `id` INT(11) NOT NULL AUTO_INCREMENT, `deptName` VARCHAR(30), `locAdd` VARCHAR(40), PRIMARY KEY (`id`) ) ENGINE=INNODB CHARSET=utf8; CREATE TABLE `db0206`.`tbl_emp`( `id` INT(11) NOT NULL AUTO_INCREMENT, `name` VARCHAR(20), `deptId` INT(11), PRIMARY KEY (`id`), FOREIGN KEY (`deptId`) REFERENCES `db0206`.`tbl_dept`(`id`) ) ENGINE=INNODB CHARSET=utf8;
插入數據spa
/*插入數據*/ 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);
'tbl_dept'表和'tbl_emp'表3d
1、內鏈接code
內鏈接文氏圖blog
select * from tbl_dept a inner join tbl_emp b on a.id=b.deptId;
2、左外鏈接get
左外鏈接文氏圖io
select * from tbl_dept a left join tbl_emp b on a.id=b.deptId;
3、右外鏈接event
右外鏈接文氏圖
select * from tbl_dept a right join tbl_emp b on a.id=b.deptId;
4、左鏈接
左鏈接文氏圖
select * from tbl_dept a left join tbl_emp b on a.id=b.deptId where b.deptId is null;
5、右鏈接
右鏈接文氏圖
select * from tbl_dept a right join tbl_emp b on a.id=b.deptId where a.id is null;
6、全鏈接
全鏈接文氏圖
select * from tbl_dept a right join tbl_emp b on a.id=b.deptId union select * from tbl_dept a left join tbl_emp b on a.id=b.deptId;
文氏圖
select * from tbl_dept a right join tbl_emp b on a.id=b.deptId union select * from tbl_dept a left join tbl_emp b on a.id=b.deptId;