/* 2 功能:生成博客目录的JS工具 3 测试:IE8,火狐,google测试通过 6 */ 7 var BlogDirectory = { 8 /* 9 获取元素位置,距浏览器左边界的距离(left)和距浏览器上边界的距离(top) 10 */ 11 getElementPosition:function (ele) { 12 var topPosition = 0; 13 var leftPosition = 0; 14 while (ele){ 15 topPosition += ele.offsetTop; 16 leftPosition += ele.offsetLeft; 17 ele = ele.offsetParent; 18 } 19 return {top:topPosition, left:leftPosition}; 20 }, 21 22 /* 23 获取滚动条当前位置 24 */ 25 getScrollBarPosition:function () { 26 var scrollBarPosition = document.body.scrollTop || document.documentElement.scrollTop; 27 return scrollBarPosition; 28 }, 29 30 /* 31 移动滚动条,finalPos 为目的位置,internal 为移动速度 32 */ 33 moveScrollBar:function(finalpos, interval) { 34 35 //若不支持此方法,则退出 36 if(!window.scrollTo) { 37 return false; 38 } 39 40 //窗体滚动时,禁用鼠标滚轮 41 window.onmousewheel = function(){ 42 return false; 43 }; 44 45 //清除计时 46 if (document.body.movement) { 47 clearTimeout(document.body.movement); 48 } 49 50 var currentpos =BlogDirectory.getScrollBarPosition();//获取滚动条当前位置 51 52 var dist = 0; 53 if (currentpos == finalpos) {//到达预定位置,则解禁鼠标滚轮,并退出 54 window.onmousewheel = function(){ 55 return true; 56 } 57 return true; 58 } 59 if (currentpos < finalpos) {//未到达,则计算下一步所要移动的距离 60 dist = Math.ceil((finalpos - currentpos)/10); 61 currentpos += dist; 62 } 63 if (currentpos > finalpos) { 64 dist = Math.ceil((currentpos - finalpos)/10); 65 currentpos -= dist; 66 } 67 68 var scrTop = BlogDirectory.getScrollBarPosition();//获取滚动条当前位置 69 window.scrollTo(0, currentpos);//移动窗口 70 if(BlogDirectory.getScrollBarPosition() == scrTop)//若已到底部,则解禁鼠标滚轮,并退出 71 { 72 window.onmousewheel = function(){ 73 return true; 74 } 75 return true; 76 } 77 78 //进行下一步移动 79 var repeat = "BlogDirectory.moveScrollBar(" + finalpos + "," + interval + ")"; 80 document.body.movement = setTimeout(repeat, interval); 81 }, 82 83 htmlDecode:function (text){ 84 var temp = document.createElement("div"); 85 temp.innerHTML = text; 86 var output = temp.innerText || temp.textContent; 87 temp = null; 88 return output; 89 }, 90 91 /* 92 创建博客目录, 93 id表示包含博文正文的 div 容器的 id, 94 mt 和 st 分别表示主标题和次级标题的标签名称(如 H2、H3,大写或小写都可以!), 95 interval 表示移动的速度 96 */ 97 createBlogDirectory:function (id, mt, st, interval){ 98 //获取博文正文div容器 99 var elem = document.getElementById(id); 100 if(!elem) return false; 101 //获取div中所有元素结点 102 var nodes = elem.getElementsByTagName("*"); 103 //创建博客目录的div容器 104 var divSideBar = document.createElement('DIV'); 105 divSideBar.className = 'sideBar'; 106 divSideBar.setAttribute('id', 'sideBar'); 107 var divSideBarTab = document.createElement('DIV'); 108 divSideBarTab.setAttribute('id', 'sideBarTab'); 109 divSideBar.appendChild(divSideBarTab); 110 var h2 = document.createElement('H2'); 111 divSideBarTab.appendChild(h2); 112 var txt = document.createTextNode('目录导航'); 113 h2.appendChild(txt); 114 var divSideBarContents = document.createElement('DIV'); 115 divSideBarContents.style.display = 'none'; 116 divSideBarContents.setAttribute('id', 'sideBarContents'); 117 divSideBar.appendChild(divSideBarContents); 118 //创建自定义列表 119 var dlist = document.createElement("dl"); 120 divSideBarContents.appendChild(dlist); 121 var num = 0;//统计找到的mt和st 122 mt = mt.toUpperCase();//转化成大写 123 st = st.toUpperCase();//转化成大写 124 //遍历所有元素结点 125 for(var i=0; i

oracle学习笔记(十三) 查询练习(三) 子查询查询

子查询练习

create table empployee_demo(
   empno     number(4) not null primary key,  --员工编号,主键
   ename     varchar2(10) not null unique,    --员工名,唯一键
   job       varchar2(9),                     --职位、工作
   mgr       number(4),                       --经理编号
   hiredate  date default sysdate,            --入职日期,默认约束
   sal       number(7,2) check(sal>=500 and sal<=10000),   --工资
   comm      number(7,2),                     --资金
   deptno    number(2)                        --部门编号
)

--department
DEPTNO NUMBER(2)    --部门编号                          
DNAME  VARCHAR2(14) Y             --部门名字             
LOC    VARCHAR2(13) Y          --部门位置

--salgrade
grade 
losal
hisal 


--39. 查询部门名称为SALES和ACCOUNTING的员工信息
select * from employee
where deptno in 
(select deptno from department 
where dname ='SALES' or dname ='ACCOUNTING')

              
--40. 查询不是经理的员工的信息(使用in 或 not in来做)
select * from employee
where job not in ('MANAGER');

--41. 查询工资比10号部门员工中任意一个低的员工信息
select * from employee
where sal < any(select sal from employee where deptno=10);

--42. 查询工资比10号部门都要低的员工信息
select * from employee
where sal < all(select sal from employee where deptno=10);

select * from employee
where sal < (select min(sal) from employee where deptno=10);
--43. 查询出部门名称,部门员工数,部门平均工资,部门最低工资雇员的姓名,及工资等级
select d.dname 部门名,emp.cou 部门员工数,emp.avgsal 平均工资,e.ename 最低工资员工名,s.grade 工资等级
from department d,
     employee e,
     salgrade s,
     (select deptno,avg(sal) avgsal,count(empno) cou,min(sal) minsal from employee 
group by deptno) emp
where d.deptno = emp.deptno and e.sal = emp.minsal and e.sal between s.losal and s.hisal

--44.找出与入职时间最早的员工在同一个部门的员工信息以及所在部门名称
select * from employee e
  left join department d on e.deptno = d.deptno  
where e.deptno = ( select deptno from employee
      where hiredate = (select min(hiredate) from employee))
      
--45. 列出最低薪金大于1500的各种工作及此从事此工作的全部雇员人数
select count(empno),job
from employee
where job in (select job from employee
  group by job
  having min(sal)>1500
  )
group by job  


--46. 求出在'SALES'部门工作的员工姓名,假设不知道销售部的部门编号
select ename from employee 
where deptno =(select deptno from department
where dname='SALES')

--47. 列出薪金高于公司平均薪金的所有员工,所在部门,上级领导,和工资等级
select e.*,s.grade,boss.ename 上级领导
from employee e,department d,salgrade s,employee boss
where e.deptno = d.deptno and e.sal between losal and hisal and e.mgr = boss.empno
and e.sal > (select avg(sal) from employee)

--48. 列出与员工SCOTT从事相同工作的所有员工及部门名称
select ename,e.job,dname from employee e,department d,(select deptno,job from employee where ename ='SCOTT') e1
where e.deptno = d.deptno and e.job = e1.job and e.deptno=e1.deptno and ename <>'SCOTT'


--49. 查询和SMITH部门相同, 岗位相同的人
select * from employee
where deptno = (select deptno from employee where ename='SMITH')
and job = (select job from employee where ename = 'SMITH')


--50. 和ALLEN同部门,工资高于MARTIN的雇员有哪些
select * from employee
where deptno = (select deptno from employee where ename='ALLEN') and
sal> (select sal from employee where ename = 'MARTIN')

--51. 比blake工资高的雇员有哪些? 
select * from employee
where sal> (select sal from employee where ename = 'BLAKE')

--52. 高于30部门最高工资的雇员有哪些?
select * from employee
where sal > (select max(sal) from employee where deptno=30)

--53. 查询scott.emp表中所有的经理的信息(此操作子查询会返回多行记录)
select * from employee  
where empno in (select distinct mgr from employee where mgr is not null);

--54. 工资高于本部门平均工资的人(拿上游工资的人)有哪些?
select deptno,ename,sal 
from employee e
where sal>(select avg(sal) from employee where deptno=e.deptno);

--55. 工作和部门与SMITH相同,工资高于JAMES的雇员有哪些?
select * from employee
where deptno = (select deptno from employee where ename='SMITH')
and job = (select job from employee where ename = 'SMITH') and sal > (select sal from employee where ename='JAMES')  

select job,deptno,sal 
from employee 
where (job,deptno)=(select job,deptno from employee where ename='SMITH') 
  and sal>(select sal from employee where ename='JAMES');
  
--56.列出每个部门工作的员工数量,平均工资和平均服务年限
select deptno 部门,count(empno) 员工数量,avg(sal) 平均工资,avg(extract(year from sysdate)-extract(year from hiredate)) 平均服务年限 from employee
group by deptno
order by deptno

--57. 列出薪金等于部门30中员工的薪金的所有员工的姓名和薪金
select ename,sal from employee
where sal in (select sal from employee where deptno = 30) and deptno!=30;

--58.列出薪金大于部门30中员工的薪金的所有员工的姓名和薪金
select ename,sal from employee
where sal > all(select sal from employee where deptno = 30) and deptno!=30;

/* 
59. 分页查询,每页显示5条记录,按工资升序排序

使用employee表,利用子查询和 rownum伪列编写一个分页查询语句,每页显示5条记录,
如第1页:第1~5条记录,第2页:第6~10条记录,.......

<a href="....?pageNo=1">第一页</a>
<a href="....?pageNo=?">上一页</a>
<a href="....?pageNo=?">下一页</a>
<a href="....?pageNo=max_value">最后一页</a>

rownum伪列是为查询结果集的行分配行号,是先有了结果后才分配的行号
如果通过主键排序rownum的行号是有序的,但如果使其字段排序则行号无序
*/
select * from(
    select t.*,rownum rn from (select * from employee order by sal ) t --每条数据都有了个伪列
    where rownum<=10 --到10行结束
) where rn>=5 --5行开始

--60. 有如下表结构,删除表中重复的数据,重复数据只保留一行:表:emp_dup(id number, name varchar2(10));
--    自行插入一些重复数据
delete from  emp_dup a  
where  a.rowid>(
    select min(b.rowid) from  emp_dup b where  a.id = b.id and a.name = b.name
);
commit;
posted @ 2019-09-19 14:37  我的人生  阅读(422)  评论(0编辑  收藏  举报