MySQL 七种 JOIN 的 SQL 编写
环境搭建
# 创建部门表 CREATE TABLE tbl_dept ( id INT NOT NULL AUTO_INCREMENT, deptName VARCHAR (30) DEFAULT NULL, locAdd VARCHAR (40) DEFAULT NULL, PRIMARY KEY (id) ) ENGINE = INNODB AUTO_INCREMENT = 1 DEFAULT chharset = utf8 ; # 创建员工表 CREATE TABLE tbl_emp ( id INT NOT NULL AUTO_INCREMENT, `name` VARCHAR (20) DEFAULT NULL, deptId INT (11) DEFAULT NULL, PRIMARY KEY (id), KEY fk_dept_id (deptId) CONSTRAINT fk_dept_id FORREIGN KEY (deptId) REFERENCES tbl_dept (id) ) ENGINE = INNODB AUTO_INCREMENT = 1 DEFAULT CHARSET = uttf8 ; # 插入部门信息 INSERT INTO tbl_dept(deptName,locAdd) VALUES('RD',11),('HR',12),('MK',13),('MIS',14),('FD',15); # 插入员工信息 INSERT INTO tbl_emp(`name`,deptId) VALUES('z3',1),('z4',1),('z5',1),('w5',2),('w6',2),('s7',3),('s8',4),('s9',51);
1.内连接(INNER JOIN)
SELECT * FROM tbl_dept a INNER JOIN tbl_emp b ON a.id = b.deptId;
有图有真相
https://www.cnblogs.com/likeyou99315/p/15172229.html