部门mysql操作
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日');
部门mysql操作的更多相关文章
- mysql操作和详解
温馨提示 mysql安装包里面:mysqld是服务端,mysql是客户端. mysqld其实是SQL后台程序(也就是MySQL服务器),它是关于服务器端的一个程序,mysqld意思是mysql dae ...
- Mysql操作初级
Mysql操作初级 本节内容 数据库概述 数据库安装 数据库操作 数据表操作 表内容操作 1.数据库概述 数据库管理系统叫做DBMS 1.什么是数据库 ? 答:数据的仓库,如:在ATM的示例中我们创建 ...
- python学习道路(day12note)(mysql操作,python链接mysql,redis)
1,针对mysql操作 SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpass'); 设置密码 update user set password ...
- 学习笔记:MySQL操作初步
对数据库的操作:SQL语言 一:SQL:Structured Query Language,结构化查询语言! 二:DDL:Data Definition Language,数据定义语言 三:DML:D ...
- ecshop的Mysql操作类
摘要,这是直接摘抄的ecshop的mysql操作类:不过他这里的缓存是用的文件缓存,我们如果想直接使用,可以替换成memcache的或者redis的! <?php /** * ECSHOP MY ...
- shell执行mysql操作
http://ully.iteye.com/blog/1226494 http://www.jb51.net/article/55207.htm shell执行mysql操作 mysql -hhos ...
- mysql操作类库--摘抄
<!--?php /** +---------------------------------- * MySQL操作类库 +---------------------------------- ...
- 第一篇:Mysql操作初级
Mysql操作初级 Mysql操作初级 本节内容 数据库概述 数据库安装 数据库操作 数据表操作 表内容操作 1.数据库概述 数据库管理系统叫做DBMS 1.什么是数据库 ? 答:数据的仓库,如: ...
- Mysql 操作手册
mysql操作手册 版本:5.6.16mysql linux安装基本步骤:#rpm -e --nodeps mysql-lib-5.1.*#rpm -ivh mysql-server#rpm -ivh ...
- Python 第九篇:队列Queue、生产者消费者模型、(IO/异步IP/Select/Poll/Epool)、Mysql操作
Mysql操作: grant select,insert,update,delete on *.* to root@"%" Identified by "123456&q ...
随机推荐
- oracle 高级队列
转载:http://www.idevelopment.info/data/Oracle/DBA_tips/Advanced_Queuing/AQ_2.shtml Overview This artic ...
- vvv动态组件和keep-alive
<!DOCTYPE html><html> <head> <style> </style> <script src="a.j ...
- PHPStudy设置局域网访问
PHPStudy是一款轻量级PHP服务器,搭建环境迅速.但是与XAMPP之类服务器不同的是,PHPStudy默认只有本机才能设置域名.访问网站.需要更改vhost.conf中的文件,才可以使得内网可以 ...
- 【C学习笔记】day2-2 不允许创建临时变量,交换两个数的内容(附加题)
#include<stdio.h> int main() { int a=0, b=1; int m[2]; m[0] = a; m[1] = b; a = m[1]; b = m[0]; ...
- 2022-04-14内部群每日三题-清辉PMP
1.项目经理资源有限,无法获得更多资源.项目经理应该使用什么技术来充分利用现有资源,而不会令项目完成时间延期? A.资源平滑 B.资源平衡 C.快速跟进 D.赶工 2.正在审查问题日志的项目经理注意到 ...
- vue的表单
你可以用 v-model 指令在表单控件元素上创建双向数据绑定. 输入框 实例中演示了 input 和 textarea 元素中使用 v-model 实现双向数据绑定: <!DOCTYPE ht ...
- 配置RMAN(缩减版)
配置备份的默认类型:备份集或副本 要配置默认备份类型: 启动 RMAN 并连接到目标数据库和恢复目录(如果使用). 将备份集或映像副本配置为默认备份类型. 以下示例配置磁盘备份到副本和备份集的备份类型 ...
- idea 常用的快捷键
1.ctrl+shitf+u 大小写切换 2.ctrl+shitf+L 快速格式化代码 3.ctrl+alt+方向左键 快速回到上一层 4..ctrl+shitf+E 最近更改的文件 5.ctrl ...
- 利用python脚本统计和删除redis key
该脚本扫描redis中所有的key,用于分析redis内存数据的key构成,扫描并保存文件,需要python支持redis模块. #!/usr/bin/env python # -*- coding: ...
- STM32工程建立
STM32工程建立 对于用keil5建立stm32工程有两种方法,一种在学习过程中比较方便的建立方式:我们称为工程方式一,另一个便是在实际工程中用的最多,也最普遍,在实际过程中用的最多的,我们称为工程 ...