use test_db; -- 删除表 drop table if exists t1_profit; drop table if exists t1_salgrade; drop table if exists t1_emp; drop table if exists t1_dept; -- 部门信息表 create table if not exists t1_dept( deptno int primary key, -- 部门编号 主键 dname varchar(10),-- 部门名称 loc varchar(10)-- 位置 ); insert into t1_dept values(10,'教研部','北京'),(20,'学工部','上海'),(30,'销售部','广州'),(40,'财务部','深圳'),(50,'卫生部','武汉'); -- 成员信息表 create table if not exists t1_emp( empno int primary key, -- 成员编号 主键 ename varchar(10) not null, -- 姓名 job varchar(10), -- 工作 mgr int, -- 上级编号 hiredate datetime, -- 雇佣日期 sal decimal(7,2), -- 薪资 comm decimal(7,2), -- 提成奖金 deptno int -- 部门编号 外键 ); /* 添加外键约束: 约束名:fk_XXX alter table 外键表的表名 add CONSTRAINT 约束名称 FOREIGN key (引用列名称) REFERENCES 主键表(主键列); */ alter table t1_emp add CONSTRAINT fk_emp_deptno FOREIGN KEY (deptno) REFERENCES t1_dept(deptno); insert into t1_emp values (1001,'甘宁','文员',1013,'2000-12-17',8000.00,NULL,20), (1002,'黛绮丝','销售员',1006,'2001-02-20',16000.00,3000.00,30), (1003,'殷天正','销售员',1006,'2001-02-22',12500.00,5000.00,30), (1004,'刘备','经理',1009,'2001-04-02',29750.00,NULL,20), (1005,'谢逊','销售员',1006,'2001-09-28',12500.00,14000.00,30), (1006,'关羽','经理',1009,'2001-05-01',28500.00,NULL,30), (1007,'张飞','经理',1009,'2001-09-01',24500.00,NULL,10), (1008,'诸葛亮','分析师',1004,'2007-04-19',30000.00,NULL,20), (1009,'曾阿牛','董事长',NULL,'2001-11-17',50000.00,NULL,10), (1010,'韦一笑','销售员',1006,'2011-09-08',15000.00,0.00,30), (1011,'周泰','文员',1008,'2007-05-23',11000.00,NULL,20), (1012,'程普','文员',1006,'2001-12-03',9500.00,NULL,30), (1013,'庞统','分析师',1004,'2001-12-03',30000.00,NULL,20), (1014,'黄盖','文员',1007,'2002-01-23',13000.00,NULL,10), (1015,'张三','保洁员',1001,'2013-05-01',80000.00,50000.00,50); -- 工资级别表 create table if not exists t1_salgrade( grade int primary key auto_increment,-- 薪资等级 losal decimal(7,2) not null,-- 该级别最低薪资 hisal decimal(7,2) not NULL -- 该级别最高薪资 ); insert into t1_salgrade values(1,7000.00,12000.00), (2,12010.00,14000.00), (3,14010.00,20000.00), (4,20010.00,30000.00), (5,30010.00,99990.00); -- 年度利润表 create table if not exists t1_profit( year year primary key,-- 年度 zz int -- 利润 ); insert into t1_profit values(2010,100),(2011,150),(2012,250),(2013,800),(2014,1000); -- 查询出姓张的,并且有提成的员工信息! select * from t1_emp where ename like '张%' and comm is not null; -- 查询出“财务部”的员工信息!【使用联表和子查询两种方式实现】 select * from t1_dept d ,t1_emp e where d.deptno=e.deptno and d.dname='财务部' GROUP BY e.empno; -- 按照工资进行降序,查询前5的员工信息。【包含部门名称】! select * from t1_dept d ,t1_emp e where d.deptno=e.deptno ORDER BY e.sal desc limit 5; -- 统计出每个部门的人数、以及最高工资和最低工资! select d.dname,count(d.deptno),max(e.sal),min(e.sal) from t1_dept d ,t1_emp e where d.deptno=e.deptno GROUP BY e.deptno; -- 查询出工资在4级别的员工信息。 select e.* from t1_emp e,t1_salgrade s where e.sal between (select losal from t1_salgrade where grade =4) and (select hisal from t1_salgrade where grade =4) GROUP BY e.empno; -- 无法显示4级别的范围 select * from t1_emp e,t1_salgrade s where e.sal between s.losal and s.hisal and s.grade = 4; -- 推荐 -- 查询出没有上级的员工信息! select * from t1_emp where mgr is null; -- 查询出支出最高的部门信息! select d.* from t1_dept d,t1_emp e where d.deptno=e.deptno GROUP BY e.deptno ORDER BY sum(ifnull(e.sal,0)+ifnull(e.comm,0)) DESC LIMIT 1; -- sum(ifnull(sal,0)+ifnull(comm,0)) -- ##查询出每个部门最高工资的员工信息! select * from t1_emp e,(select max(sal) max,deptno from t1_emp GROUP BY deptno) s where e.deptno = s.deptno and e.sal = s.max; select * from t1_emp e where (e.deptno,e.sal) in (select deptno,max(sal) from t1_emp group by deptno); -- ##查出至少有一个员工的部门。显示部门编号、部门名称、部门位置、部门人数。 select d.*,count(e.deptno) from t1_dept d,t1_emp e where d.deptno=e.deptno GROUP BY d.deptno ;-- 没人就是null 直接不显示 ,显示要用left join -- 列出所有员工的姓名及其直接上级的姓名。 select e.ename '员工',p.ename '上级' from t1_emp e,t1_emp p where e.mgr=p.empno; -- 没有曾阿牛 select e.ename '员工',p.ename '上级' from t1_emp e left join t1_emp p on e.mgr=p.empno; -- 列出受雇日期早于直接上级的所有员工的编号、姓名、部门名称。 select e.empno,e.ename,d.dname from t1_dept d,t1_emp e,t1_emp p where d.deptno=e.deptno and e.mgr=p.empno and e.hiredate<p.hiredate; -- ##列出部门名称和这些部门的员工信息,同时列出那些没有员工的部门。 select d.dname,e.* from t1_dept d left join t1_emp e on d.deptno=e.deptno left join (select d.deptno from t1_dept d left join t1_emp e on d.deptno=e.deptno where e.empno is null) s on s.deptno=d.deptno; select * from t1_dept d left join t1_emp e on d.deptno=e.deptno; -- left join展示null -- 列出最低薪金大于15000的各种工作及从事此工作的员工人数。 select e.job,count(e.job) from t1_emp e, (select job from t1_emp GROUP BY job having min(sal)>15000 ) s where e.job=s.job GROUP BY e.job; select job,min(sal),count(job) from t1_emp GROUP BY job having min(sal)>15000; -- 列出在销售部工作的员工的姓名,假定不知道销售部的部门编号。 select e.ename from t1_emp e,(select deptno from t1_dept where dname='销售部') s where e.deptno=s.deptno; select * from t1_emp where deptno = ( select deptno from t1_dept where dname = '销售部' ); ############################### -- 列出薪金高于公司平均薪金的所有员工信息,所在部门名称,上级领导,工资等级。 select e.*,d.dname,p.ename,s.grade from t1_dept d,t1_emp e,t1_emp p,t1_salgrade s, (select avg(sal) sals from t1_emp) a where e.sal >a.sals and d.deptno=e.deptno and e.mgr = p.empno and e.sal BETWEEN s.losal and s.hisal GROUP BY e.empno; -- 没有曾阿牛 select e.*,d.dname,p.ename,s.grade from t1_dept d,t1_emp e,t1_emp p,t1_salgrade s where e.deptno=d.deptno and e.mgr=p.empno and e.sal between s.losal and s.hisal and e.sal>( select avg(sal) from t1_emp );-- 内连接 select e.*,d.dname,p.ename,s.grade from t1_emp e left join t1_dept d on e.deptno=d.deptno left join t1_emp p on e.mgr = p.empno left join t1_salgrade s on e.sal between s.losal and s.hisal where e.sal>( select avg(sal) from t1_emp ); -- 曾阿牛没有上级,显示 ############################### -- 列出与庞统从事相同工作的所有员工及部门名称。 select e.ename,d.dname from t1_dept d,t1_emp e, (select deptno from t1_emp where ename='庞统') s where d.deptno=e.deptno and d.deptno=s.deptno ;-- 相同部门 select* from t1_dept d,t1_emp e where e.deptno=d.deptno and e.job=( select job from t1_emp where ename='庞统' );-- 相同工作 -- 列出薪金高于在部门30工作的所有员工的薪金的员工姓名和薪金、部门名称。 select e.ename,e.sal,d.dname from t1_dept d,t1_emp e, (select max(sal) max from t1_emp where deptno=30) m where d.deptno=e.deptno and e.sal>m.max; -- 查出年份、利润、年度增长比。 select t.year '年份',t.zz '利润',concat(CEIL((t.zz-p.zz)/p.zz*100),'%') '年度增长比' from t1_profit p,t1_profit t where t.year=p.year+1; -- cell向上取整,concat拼接 select rpad('asc',10,'23'); select lpad('ascv',8,'12'); select datediff('2022-10-01','2022-12-09'); select date_format(now(),'%Y年%m月%d日');
标签:--,t1,部门,emp,mysql,操作,deptno,where,select From: https://www.cnblogs.com/19981206-zxb/p/16969298.html