我们在开发中,根据不同的业务需求往往需要通过2张及以上的表中去查询需要的数据。所以我们有必要学习2张及以上的表的查询。其实不管是几张表的查询,都是有规律可循的。
1.1 准备数据
-- 部门表
CREATE TABLE dept (
id INT PRIMARY KEY PRIMARY KEY, -- 部门id
dname VARCHAR(50), -- 部门名称
loc VARCHAR(50) -- 部门位置
);
-- 添加4个部门
INSERT INTO dept(id,dname,loc) VALUES
(10,'教研部','北京'),
(20,'学工部','上海'),
(30,'销售部','广州'),
(40,'财务部','深圳');
-- 职务表,职务名称,职务描述
CREATE TABLE job (
id INT PRIMARY KEY,
jname VARCHAR(20),
description VARCHAR(50)
);
-- 添加4个职务
INSERT INTO job (id, jname, description) VALUES
(1, '董事长', '管理整个公司,接单'),
(2, '经理', '管理部门员工'),
(3, '销售员', '向客人推销产品'),
(4, '文员', '使用办公软件');
-- 员工表
CREATE TABLE emp (
id INT PRIMARY KEY, -- 员工id
ename VARCHAR(50), -- 员工姓名
job_id INT, -- 职务id
mgr INT , -- 上级领导
joindate DATE, -- 入职日期
salary DECIMAL(7,2), -- 工资
bonus DECIMAL(7,2), -- 奖金
dept_id INT, -- 所在部门编号
CONSTRAINT emp_jobid_ref_job_id_fk FOREIGN KEY (job_id) REFERENCES job (id),
CONSTRAINT emp_deptid_ref_dept_id_fk FOREIGN KEY (dept_id) REFERENCES dept (id)
);
-- 添加员工
INSERT INTO emp(id,ename,job_id,mgr,joindate,salary,bonus,dept_id) VALUES
(1001,'孙悟空',4,1004,'2000-12-17','8000.00',NULL,20),
(1002,'卢俊义',3,1006,'2001-02-20','16000.00','3000.00',30),
(1003,'林冲',3,1006,'2001-02-22','12500.00','5000.00',30),
(1004,'唐僧',2,1009,'2001-04-02','29750.00',NULL,20),
(1005,'李逵',4,1006,'2001-09-28','12500.00','14000.00',30),
(1006,'宋江',2,1009,'2001-05-01','28500.00',NULL,30),
(1007,'刘备',2,1009,'2001-09-01','24500.00',NULL,10),
(1008,'猪八戒',4,1004,'2007-04-19','30000.00',NULL,20),
(1009,'罗贯中',1,NULL,'2001-11-17','50000.00',NULL,10),
(1010,'吴用',3,1006,'2001-09-08','15000.00','0.00',30),
(1011,'沙僧',4,1004,'2007-05-23','11000.00',NULL,20),
(1012,'李逵',4,1006,'2001-12-03','9500.00',NULL,30),
(1013,'小白龙',4,1004,'2001-12-03','30000.00',NULL,20),
(1014,'关羽',4,1007,'2002-01-23','13000.00',NULL,10);
-- 工资等级表
CREATE TABLE salarygrade (
grade INT PRIMARY KEY,
losalary INT,
hisalary INT
);
-- 添加5个工资等级
INSERT INTO salarygrade(grade,losalary,hisalary) VALUES
(1,7000,12000),
(2,12010,14000),
(3,14010,20000),
(4,20010,30000),
(5,30010,99990);
1.2 练习
1.2.1 练习1
查询所有员工信息。显示员工编号,员工姓名,工资,职务名称,职务描述
具体操作: 1.确定要查询哪些表:emp e, job j
SELECT * FROM emp e INNER JOIN job j;
2.确定表连接条件: e.job_id=j.id
SELECT * FROM emp e INNER JOIN job j ON e.job_id=j.id;
3.确定查询字段:员工编号,员工姓名,工资,职务名称,职务描述
显示内连接查询
SELECT e.`id`, e.`ename`, e.`salary`, j.`jname`, j.`description`
FROM emp e
INNER JOIN job j ON e.job_id=j.id;
也可以: 隐式内连接查询
SELECT e.id,e.ename,e.salary,j.jname,j.description
FROM emp e,job j
WHERE e.job_id=j.id
1.2.2 练习2
查询所有员工信息。显示员工编号,员工姓名,工资,职务名称,职务描述,部门名称,部门位置
具体操作: 1. 确定要查询哪些表,emp e, job j, dept d
SELECT * FROM emp e INNER JOIN job j INNER JOIN dept d;
2. 确定表连接条件 e.job_id=j.id and e.dept_id=d.id
SELECT *
FROM emp e
INNER JOIN job j
INNER JOIN dept d
ON e.job_id=j.id AND e.dept_id=d.id;
3. 确定查询字段:员工编号,员工姓名,工资,职务名称,职务描述,部门名称,部门位置
SELECT e.`id`, e.`ename`, e.`salary`, j.`jname`, j.`description`, d.`dname`, d.`loc`
FROM emp e
INNER JOIN job j
INNER JOIN dept d
ON e.job_id=j.id AND e.dept_id=d.id;
SELECT e.`id`, e.`ename`, e.`salary`, j.`jname`, j.`description`, d.`dname`, d.`loc`
FROM emp e,job j ,dept d
WHERE e.job_id=j.id AND e.dept_id=d.id;
1.2.3 练习3
查询所有员工信息。显示员工姓名,工资,职务名称,职务描述,部门名称,部门位置,工资等级
具体操作: 1. 确定要查询哪些表,emp e, job j, dept d, salarygrade s
SELECT * FROM emp e INNER JOIN job j INNER JOIN dept d INNER JOIN salarygrade s;
2. 确定表连接条件 e.job_id=j.id and e.dept_id=d.id and e.salary between s.losalary and hisalary
SELECT *
FROM emp e
INNER JOIN job j
INNER JOIN dept d
INNER JOIN salarygrade s
ON e.job_id=j.id AND e.dept_id=d.id AND e.salary
BETWEEN s.losalary AND s.hisalary;
3. 确定查询字段:员工姓名,工资,职务名称,职务描述,部门名称,部门位置,工资等级
SELECT e.`ename`, e.`salary`, j.`jname`, j.`description`, d.`dname`, d.`loc`, s.`grade`
FROM emp e
INNER JOIN job j
INNER JOIN dept d
INNER JOIN salarygrade s
ON e.job_id=j.id AND e.dept_id=d.id AND e.salary
BETWEEN s.losalary AND hisalary;
SELECT e.`ename`, e.`salary`, j.`jname`, j.`description`, d.`dname`, d.`loc`, s.`grade`
FROM emp e ,job j ,dept d ,salarygrade s
WHERE e.job_id=j.id AND e.dept_id=d.id AND e.salary
BETWEEN s.losalary AND hisalary;
1.2.3.1 多表查询规律总结
-
不管我们查询几张表,表连接查询会产出笛卡尔积,我们需要消除笛卡尔积,拿到正确的数据。我们需要找到表与表之间通过哪个字段关联起来的(通常是
外键=主键
) -
消除笛卡尔积规律:2张表需要1个条件,3张表需要2个条件,4张表需要3个条件。(条件数量=表的数量-1),每张表都要参与进来
-
多表连接查询步骤: 3.1. 确定要查询哪些表 3.2. 确定表连接条件 3.3. 确定查询字段
1.2.4 练习4
查询经理的信息。显示员工姓名,工资,职务名称,职务描述,部门名称,部门位置,工资等级
具体操作: 1. 确定要查询哪些表,emp e, job j, dept d, salarygrade s
SELECT * FROM emp e INNER JOIN job j INNER JOIN dept d INNER JOIN salarygrade s;
2. 确定表连接条件 e.job_id=j.id and e.dept_id=d.id and e.salary between s.losalary and hisalary
SELECT *
FROM emp e
INNER JOIN job j
INNER JOIN dept d
INNER JOIN salarygrade s
ON e.job_id=j.id AND e.dept_id=d.id AND e.salary
BETWEEN s.losalary AND hisalary;
额外条件:只需要查询经理的信息(j.jname='经理')
SELECT e.`ename`, e.`salary`, j.`jname`, j.`description`, d.`dname`, d.`loc`, s.`grade`
FROM emp e
INNER JOIN job j
INNER JOIN dept d
INNER JOIN salarygrade s
ON e.job_id=j.id AND e.dept_id=d.id AND e.salary
BETWEEN s.losalary AND hisalary AND j.jname='经理';
SELECT e.`ename`, e.`salary`, j.`jname`, j.`description`, d.`dname`, d.`loc`, s.`grade`
FROM emp e ,job j ,dept d ,salarygrade s
WHERE e.job_id=j.id
AND e.dept_id=d.id
AND e.salary
BETWEEN s.losalary AND hisalary
AND j.jname="经理";
1.2.5 练习5
查询出部门编号、部门名称、部门位置、部门人数
具体操作:
1. 去员工表中找到每个部门的人数和部门idSELECT dept_id, COUNT(*) FROM emp GROUP BY dept_id;
SELECT *
FROM dept d
INNER JOIN
(SELECT dept_id, COUNT(*) FROM emp GROUP BY dept_id) e ON e.dept_id=d.`id`;
SELECT d.`id`, d.dname, d.`loc`, e.total 部门人数
FROM dept d
INNER JOIN
(SELECT dept_id, COUNT(*) total FROM emp GROUP BY dept_id) e
ON e.dept_id=d.`id`;
SELECT d.*, IFNULL(e.total,0) '部门人数'
FROM dept d
LEFT JOIN
(SELECT dept_id, COUNT(id) 'total' FROM emp GROUP BY dept_id) e
ON e.dept_id=d.`id`;
1.2.6 练习6
查询所有员工信息。显示员工信息和部门名称,没有员工的部门也要显示
具体操作: 1. 确定要查询哪些表,emp e, dept d
SELECT * FROM emp e INNER JOIN dept d;
SELECT * FROM emp e RIGHT JOIN dept d ON e.dept_id=d.id;
注意:没有员工的部门也要显示。右边数据要全部显示所以使用右外连接
SELECT e.*, d.`dname`
FROM emp e
RIGHT JOIN dept d ON e.dept_id=d.id;
1.2.7 练习7
查询所有员工信息。显示员工姓名,员工工资,职务名称,工资等级,并按工资升序排序
具体操作: 1. 确定要查询哪些表,emp e, job j, salarygrade s
SELECT *
FROM emp e
INNER JOIN job j INNER JOIN salarygrade s;
2. 确定表连接条件 e.job_id=j.id and e.salary between s.losalary and s.hisalary
SELECT *
FROM emp e
INNER JOIN job j
INNER JOIN salarygrade s
ON e.job_id=j.id AND e.salary
BETWEEN s.losalary AND s.hisalary;
3. 确定查询字段:员工姓名,员工工资,工资等级,并按工资升序排序
SELECT e.`ename`, j.`jname`, e.`salary`, s.`grade`
FROM emp e
INNER JOIN job j
INNER JOIN salarygrade s
ON e.job_id=j.id AND e.salary
BETWEEN s.losalary AND s.hisalary ORDER BY e.`salary`;
1.2.8 练习8
列出所有员工的姓名及其直接上级的姓名,没有领导的员工也需要显示
具体操作:
1. 确定要查询哪些表,emp e, emp m
SELECT * FROM emp e INNER JOIN emp m;
2. 确定表连接条件 e.mgr=e2.id
SELECT * FROM emp e INNER JOIN emp m ON e.`mgr`=m.`id`;
SELECT * FROM emp e LEFT JOIN emp m ON e.`mgr`=m.`id`;
**我们发现少了一条数据,因为罗贯中是董事长没有上司**
没有领导的员工也需要显示,所以左表的数据需要全部显示。使用左外连接
3. 确定查询字段:员工的姓名及其直接上级的姓名
SELECT e.`ename`, IFNULL(m.`ename`, '没有') 上司
FROM emp e LEFT JOIN emp m ON e.`mgr`=m.`id`;
1.2.9 练习9
查询入职期早于直接上级的所有员工编号、姓名、部门名称
具体操作: 1. 确定要查询哪些表,emp e, emp m, dept d
SELECT * FROM emp e INNER JOIN emp m INNER JOIN dept d;
2. 确定表连接条件 e.mgr=m.id and e.dept_id=d.id and e.dept_id=d.id and e.joindate<m.joindate
SELECT *
FROM emp e
INNER JOIN emp m
INNER JOIN dept d
ON e.mgr=m.id AND e.dept_id=d.id AND e.joindate<m.joindate;
3. 确定查询字段:员工编号、姓名、部门名称
SELECT e.`id`, e.`ename`, d.`dname`
FROM emp e
INNER JOIN emp m
INNER JOIN dept d
ON e.mgr=m.id AND e.joindate<m.joindate AND e.`dept_id`=d.`id`;
1.2.10 练习10
查询工资高于公司平均工资的所有员工信息。显示员工信息,部门名称,上级领导,工资等级
具体操作: 先统计公司平均工资
SELECT AVG(salary) FROM emp;
-
确定要查询哪些表,emp e, emp m, dept d, salarygrade s
SELECT *
FROM emp e
INNER JOIN emp m
INNER JOIN dept d
INNER JOIN salarygrade s;
SELECT *
FROM emp e
INNER JOIN emp m
INNER JOIN dept d
INNER JOIN salarygrade s
WHERE e.dept_id=d.id AND e.mgr=m.id AND e.salary
BETWEEN s.losalary AND hisalary AND e.salary>(SELECT AVG(salary) FROM emp);
3. 确定查询字段:员工信息,部门名称,上级领导,工资等级。
SELECT e.*, d.`dname`, m.`ename`, s.`grade`
FROM emp e
INNER JOIN emp m
INNER JOIN dept d
INNER JOIN salarygrade s
WHERE e.dept_id=d.id AND e.mgr=m.id AND e.salary
BETWEEN s.losalary AND hisalary AND e.salary>(SELECT AVG(salary) FROM emp);