环境准备
create table dept(
id int PRIMARY KEY,
dname VARCHAR(50),
loc VARCHAR(50)
);
insert into dept values
(10,'研发部','北京'),
(20,'学工部', '上海'),
(30,'销售部','广州' ),
(40,'财务部','深圳');
create table job(
id int PRIMARY KEY,
jname VARCHAR(20),
descripition VARCHAR(50)
);
INSERT INTO job VALUES
(1,'董事长','管理整个公司接单'),
(2,'经理','管理部门员工'),
(3,'销售员','向客人推销产品'),
(4,'文员','使用办公软件');
CREATE TABLE salarygrade(
grade int primary key,
losalary int,
hisalary int
);
insert into
salarygrade
VALUES
(1,7000,12000),
(2,12000,14000),
(3,14000,20010),
(4,20010,30010),
(5,30010,99990)
;
create table emp(
id int PRIMARY KEY,
ename VARCHAR(50),
job_id INT,
mgr INT,
joindate date,
salary DECIMAL(7,2),
bonus DECIMAL(7,2),
dept_id INT,
FOREIGN KEY(job_id) REFERENCES job(id),
FOREIGN KEY(dept_id) REFERENCES dept(id)
);
insert into emp values
(1001,'孙悟空',4,1004,'2000-12-17','8000.00',NULL,20),
(1002,'卢俊义',3,1006,'2001-02-20','16000.00','3000.00',30),
(100,'林冲',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);
1.查询所有员工信息,查询员工编号,员工姓名,工资,职务名称,职务描述
select
t1.id,t1.ename,t1.salary,t2.jname,t2.descripition
from
emp t1,job t2
where
t1.job_id=t2.id
;
2.查询员工编号,员工名称,工资,职务名称,职务描述,部门名称,部门位置
//分析:
//1.员工编号,员工名称,工资 emp
//2.职务名称,职务描述 job
//3.部门名称,部门位置 dept
//条件:
//1.emp.job_id=job.id
//2.emp.dept_id=dept.id
select
t1.id,t1.ename,t1.salary,
t2.jname,t2.descripition,
t3.dname,t3.loc
from
emp t1,job t2, dept t3
where
t1.job_id=t2.id and t1.dept_id=t3.id
;
3.查询员工名称,工资,工资等级
//分析:
//1.员工名称, 工资 emp
//2.工资等级 salarygrade
//条件:
//1.emp.salary >= salarygrade.losalary and emp.salary <= salarygrade.hisalary
//或者 emp.salary between salarygrade.losalary and salarygrade.hisalary
select
t1.ename,t1.salary,t2.grade
from
emp t1,salarygrade t2
where
t1.salary between t2.losalary and t2.hisalary
;
4.查询员工名称,工资,职务名称,职务描述,部门名称,部门位置,工资等级
//分析:
//1.员工名称,工资 emp
//2.职务名称,职务描述 job
//3.部门名称,部门位置 dept
//4.工资等级 salarygrade
//条件:
//1.emp.job_id=job.id
//2.emp.dept_id=dept.id
//3.emp.salary between salarygrade.losalary and salarygrade.hisalary
select
t1.ename,t1.salary,t2.jname,t2.descripition,t3.dname,t3.loc,t4.grade
from
emp t1,job t2,dept t3,salarygrade t4
where
t1.job_id=t2.id and t1.dept_id=t3.id and t1.salary between t4.losalary and t4.hisalary
;
5.查询出部门编号,部门名称,部门位置,部门人数
//分析:
//1.部门名称,部门位置 dept
//2.部门人数 emp
//条件
//1.根据emp.dept_id进行分组,查询count(id)
//2.使用子查询将上一步的结果和dept表进行关联查询
select
t1.id,t1.dname,t1.loc,t2.total
from
dept t1,
(select dept_id,count(id) total from emp group by dept_id) t2
where
t1.id = t2.dept_id
;
6.查询所有员工的姓名及其直接上级的名称,没有领导的员工也需要查询
//分析:
//1.员工的姓名 emp->t1
//2.直接上级的名称 emp->t2
//条件
//1.t1.mgr=t2.id
//2.左外连接
select
t1.id, t1.ename,t2.id, t2.ename
from
emp t1
left join
emp t2
on
t1.mgr=t2.id
;
点击返回 MySQL 快速学习目录