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操作的更多相关文章

  1. mysql操作和详解

    温馨提示 mysql安装包里面:mysqld是服务端,mysql是客户端. mysqld其实是SQL后台程序(也就是MySQL服务器),它是关于服务器端的一个程序,mysqld意思是mysql dae ...

  2. Mysql操作初级

    Mysql操作初级 本节内容 数据库概述 数据库安装 数据库操作 数据表操作 表内容操作 1.数据库概述 数据库管理系统叫做DBMS 1.什么是数据库 ? 答:数据的仓库,如:在ATM的示例中我们创建 ...

  3. python学习道路(day12note)(mysql操作,python链接mysql,redis)

    1,针对mysql操作 SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpass'); 设置密码 update user set password ...

  4. 学习笔记:MySQL操作初步

    对数据库的操作:SQL语言 一:SQL:Structured Query Language,结构化查询语言! 二:DDL:Data Definition Language,数据定义语言 三:DML:D ...

  5. ecshop的Mysql操作类

    摘要,这是直接摘抄的ecshop的mysql操作类:不过他这里的缓存是用的文件缓存,我们如果想直接使用,可以替换成memcache的或者redis的! <?php /** * ECSHOP MY ...

  6. shell执行mysql操作

    http://ully.iteye.com/blog/1226494 http://www.jb51.net/article/55207.htm shell执行mysql操作 mysql  -hhos ...

  7. mysql操作类库--摘抄

    <!--?php /** +---------------------------------- * MySQL操作类库 +---------------------------------- ...

  8. 第一篇:Mysql操作初级

    Mysql操作初级   Mysql操作初级 本节内容 数据库概述 数据库安装 数据库操作 数据表操作 表内容操作 1.数据库概述 数据库管理系统叫做DBMS 1.什么是数据库 ? 答:数据的仓库,如: ...

  9. Mysql 操作手册

    mysql操作手册 版本:5.6.16mysql linux安装基本步骤:#rpm -e --nodeps mysql-lib-5.1.*#rpm -ivh mysql-server#rpm -ivh ...

  10. Python 第九篇:队列Queue、生产者消费者模型、(IO/异步IP/Select/Poll/Epool)、Mysql操作

    Mysql操作: grant select,insert,update,delete on *.* to root@"%" Identified by "123456&q ...

随机推荐

  1. react backend uploadfile

    public List<string> WriteFile(List<UploadDTO> uploads) { List<string> fileNames = ...

  2. 易语言json

    1.易语言助手  ->网页功能->网页调试 打开post工具 提交协议头里面 右键可以自定义默认的十几个ua头 2.项目爬模发送数据格式为[{},{},{},{}]  相当于没有键 只有值 ...

  3. TypeScript - 继承(extends) - 抽象类(abstract)

    (function () { /** * abstract 开头的是抽象类, * 抽象类和其他区别不大,只是不能用来创建对象 * 抽象类专门是让别人继承的的类 */ abstract class An ...

  4. 转L:[JAVA基础] 成员变量和局部变量(一看就懂的总结归纳篇)

    [JAVA基础] 成员变量和局部变量(一看就懂的总结归纳篇) JVM--Java虚拟机详解

  5. ESP-Example ble-ancs解析

    苹果通知中心服务(ANCS)的目的是让蓝牙配件(通过蓝牙低能耗链接连接到iOS设备)以简单方便的方式访问iOS设备上产生的各种通知. Apple通知中心服务是主服务,其UUID为7905F431-B5 ...

  6. 为知笔记快速隐藏左侧 快捷键 Esc

    为知笔记快速隐藏左侧 快捷键 Esc

  7. Win10家庭版找不到组策略gpedit.msc怎么解决?

    链接:https://pan.baidu.com/s/1SoSWCfHwZhD3tV4C7DcirA 提取码:okfm 1.下载文件 2.以管理员身份运行 3.

  8. Java面向对象编程:多态(自我理解)

    多态 (1)概念:同一个行为具有多个不同表现形式或形态的能力:就是同一个接口,使用不同的实例而执行不同的操作. (2)优点:消除类型之间的耦合关系:可替换性:可扩充性:接口性:灵活性:简化性: (3) ...

  9. spring session + redis实现共享session

    一.代码 1.pom.xml <!--spring session--> <dependency> <groupId>org.springframework.boo ...

  10. HTML完整语法学习

    https://www.cnblogs.com/douluo/archive/2021/11/20/15582217.html