JAVA程序设计(20)-----查询信息的数据库代码

php中文网
发布: 2016-06-07 16:13:16
原创
1329人浏览过

增删改查 据说查询是最困难的……各种组合查询 联表查询 #0. 查询最高工资及其对应员工姓名select ename, sal from empwhere sal=(select max(sal) from emp);#如果有多个员工都是最高工资下面的方式将失效select ename, sal from emp ORDER BY sal desc lim

增删改查 据说查询是最困难的……各种组合查询 联表查询

启科网络PHP商城系统
启科网络PHP商城系统

启科网络商城系统由启科网络技术开发团队完全自主开发,使用国内最流行高效的PHP程序语言,并用小巧的MySql作为数据库服务器,并且使用Smarty引擎来分离网站程序与前端设计代码,让建立的网站可以自由制作个性化的页面。 系统使用标签作为数据调用格式,网站前台开发人员只要简单学习系统标签功能和使用方法,将标签设置在制作的HTML模板中进行对网站数据、内容、信息等的调用,即可建设出美观、个性的网站。

启科网络PHP商城系统 0
查看详情 启科网络PHP商城系统

#0. 查询最高工资及其对应员工姓名
select ename, sal from emp
where sal=(select max(sal) from emp);

#如果有多个员工都是最高工资下面的方式将失效
select ename, sal from emp ORDER BY sal desc limit 0, 1;

#补充1:能否不使用聚合函数查出最高工资及其对应员工姓名
select ename, sal from emp
where sal=(select sal from emp order by sal desc limit 0,1);

#补充2:既不用排序也不用聚合函数查出最高工资及其对应员工姓名
select ename, sal from emp
where sal not in 
(select distinct t1.sal from emp as t1
inner join emp as t2 on t1.sal<t2.sal);


#1. 计算每位员工的年薪
select ename as 姓名, (sal+if(comm is null, 0, comm))*12 as 年薪 
from emp order by 年薪 DESC;

#2. 统计有员工的部门的人数
select dname as 部门名称, 总人数 from
(select dno, count(dno) as 总人数 from emp
group by dno) as t1, dept as t2
where t1.dno=t2.dno;	#没有联接条件将产生笛卡尔积

SELECT dname as 部门名称, 总人数 FROM
(select dno, count(dno) as 总人数 from emp
group by dno) as t1 INNER JOIN dept as t2
on t1.dno=t2.dno;

#补充:把没有员工的部门也显示出来
SELECT dname as 部门名称, if(total is null, 0, total) as 总人数 
FROM (select dno, count(dno) as total from emp
group by dno) as t1 RIGHT JOIN dept as t2
on t1.dno=t2.dno;

SELECT dname as 部门名称, if(total is null, 0, total) as 总人数 
FROM dept as t2 LEFT JOIN 
(select dno, count(dno) as total from emp group by dno) as t1
on t1.dno=t2.dno;

#3.【本文来自鸿网互联 (http://www.68idc.cn)】 求挣最高薪水的员工(boss除外)的姓名
select ename, sal from emp
where sal=(select max(sal) from emp where mgr is not null);

#4. 查询薪水超过平均薪水的员工的姓名和工资
select ename, sal
from emp where sal>(select avg(sal) from emp);

#5. 查询薪水超过其所在部门平均薪水的员工的姓名、部门名称和工资
#where写法
select ename, dname, t3.sal from
(select eno, t1.dno, sal from emp as t1,
(select dno, avg(sal) as avgSal from emp group by dno) as t2
where t1.dno=t2.dno and sal>avgSal) as t3, emp as t4, dept as t5 
where t3.eno=t4.eno and t5.dno=t3.dno;

#inner join写法
select ename, dname, t3.sal from
(select eno, t1.dno, sal from emp as t1 inner join
(select dno, avg(sal) as avgSal from emp group by dno) as t2
on t1.dno=t2.dno and sal>avgSal) as t3 inner join 
emp as t4 on t3.eno=t4.eno inner join 
dept as t5 on t5.dno=t3.dno;

#6. 查询部门中薪水最高的人姓名、工资和所在部门名称
select ename, dname, t3.sal from
(select eno, t1.dno, sal from emp as t1 inner join
(select dno, max(sal) as maxSal from emp group by dno) as t2
on t1.dno=t2.dno and sal=maxSal) as t3 inner join 
emp as t4 on t3.eno=t4.eno inner join 
dept as t5 on t5.dno=t3.dno;

#7. 哪些人是主管
select * from emp 
where eno in 
(select distinct mgr from emp);

select * from emp 
where eno=any(select distinct mgr from emp);

#补充:哪些人不是主管
select * from emp 
where eno not in (select distinct mgr from emp where mgr is not null);

#8. 求平均薪水最高的部门的名称和平均工资
select dname as 部门名称, avgSal as 平均工资 from
(select dno, avgSal
from (select dno, avg(sal) as avgSal from emp
group by dno) t1 
where avgSal=(select max(avgSal) from 
(select dno, avg(sal) as avgSal from emp group by dno) as t2)) as t3
inner join dept as t4 on t3.dno=t4.dno;

#9. 求薪水最高的前3名雇员
select * from emp order by sal desc limit 0,3;

#10.求薪水排在第4-6名雇员
select * from emp order by sal desc limit 3,3;
登录后复制


相关标签:
java速学教程(入门到精通)
java速学教程(入门到精通)

java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号