部门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 ...
随机推荐
- Java项目引入第三方Jar包
普通java Project 引入jar包: 1,copy jar to lib folder. 2, imported with Build path -> Add external jars ...
- ubuntu扩容
VMware虚拟机 Linux系统 Ubuntu 16.04 硬盘/磁盘扩容效果查看硬盘大小及使用情况 终端:df -h 没有扩容前: 成功扩容后: 主要流程扩展硬盘大小到100G将未分配的60G分配 ...
- Windows安装MySQL5.7配置
1.下载对应版本安装包,http://dev.mysql.com/downloads/mysql 2.将安装包解压 3.解压后会发现没有my.ini文件,此版本并不需要手动创建my.ini文件,手动创 ...
- 统计学习导论之R语言应用(二):R语言基础
统计学习导论(ISLR) 参考资料 The Elements of Statistical Learning An Introduction to Statistical Learning 统计学习导 ...
- Path类,文件操作的路径用法
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...
- (linux笔记)开放防火墙端口
关闭防火墙 CentOS 7.RedHat 7 之前的 Linux 发行版防火墙开启和关闭( iptables ): 即时生效,重启失效 #开启 service iptables start #关闭 ...
- hadoop模板虚拟机配置
在安装好虚拟机软件后,进行IP配置 配置windows系统的ip 配置Vmware的ip 配置虚拟机的ip 首先 输入su root切换至root身份. 然后配置ip和网关 vim /etc/sysc ...
- 攻防世界Web篇——PHP2
可以从index.phps中找到网站源码 从源码中得出,要满足admin!=$_GET[id],urldecode($_GET[id]) == admin,两个条件才能得到flag 所以就将admin ...
- MarkDown学习笔记1
# MarkDown学习(一级标题)## 标题(二级)### 三级标题#### 四级标题## 字体*Hello,World!斜体***Hello,World!加粗*****Hello,World!加粗 ...
- js 导出excle文件(多页)
--前提:页面生成相应的表格数据 例如: <table id="a"> <tr> <th></th> </tr> ...