分页查询和交并集

来源:互联网 发布:淘宝外包多少钱 编辑:程序博客网 时间:2024/04/20 21:17
-- 单表查询
select 字段,函数(字段)
from 实表 u,(虚表) v
where 字段,函数(字段)
group by 字段
having 聚合函数
order by 字段,结束语,只能放当前查询语句最后面


-- 按组统计一下各部门的总工资sum,avg,max,min,count
-- group by 后面是什么字段,就只能查什么字段+聚合函数
-- 分组的情况下,对分组筛选,having只能接聚合函数,别名都不行
select deptno,sum(sal+nvl(comm,0)) salary
from emp
group by deptno;


-- 按组统计一下各部门中大于8000的总工资sum,avg,max,min,count
select deptno,sum(sal+nvl(comm,0)) salary
from emp
group by deptno
having sum(sal+nvl(comm,0))>8000;


mytable
-- 按组统计一下各部门中大于8000的总工资的部门名字与位置
select dname,loc from dept,
(
 select deptno,sum(sal+nvl(comm,0)) salary
from emp
group by deptno
having sum(sal+nvl(comm,0))>8000
)
mytable
where dept.deptno = mytable.deptno;


联合查询语法:
select 字段... from 实表 实表别名,(查询语句) 虚拟别名
where 实表别名.主键或者外键 = 虚拟别名.主键或者外键


-- 将上面的这个联合,改成inner join
----inner join,两个表之间的逗号换为inner join        。 where换为on
select dname,loc from
dept d inner join (select deptno,sum(sal+nvl(comm,0)) from emp group by deptno having sum(sal+nvl(comm,0))>8000) mytable
on d.deptno=mytable.deptno;


------------------------------------------------------------------------------------------------------------------------




-- 多表查询,联合查询
-- 等值连接:主键=外键
-- 查询出每个员工名字ename及它所在单位的名字dname
select e.ename,d.dname from emp e,dept d
where d.deptno = e.deptno;


-- 非等值连接
select e.ename,e.empno,sg.grade from emp e,salgrade sg
where sal between losal and hisal;


-- 2
select e.ename,e.empno,sg.grade from emp e,salgrade sg
where sal>= sg.losal and sal<=sg.hisal;


-- 自己连接 是  单表查询
-- e1用于表示员工
-- e2用于表示领导
select e1.empno,e2.ename from emp e1,emp e2 where e1.mgr = e2.empno;


-- 复制一张表(表结构包括数据)
create table temp as select * from emp;
-- 测试 select * from temp union select * from emp;


下面的内容全部是数据的并,不是字段并 ,只适合同一张表;
union 不重复,重复的值显示一次, 并集,所有的内容都查询,重复的显示一次
union all 包括重复   并集,所有的内容都显示,包括重复的
intersect 交集       交集:只显示重复的
minus 减集           差集:只显示对方没有的(跟顺序是有关系的)


-- 子查询:如果条件需要查询出来的查询
-- 查询出部门号为20的员工名字
select ename from emp where deptno = 20;
--查询出部门名为'RESEARCH'的员工名字
select ename from emp where deptno =
(select deptno from dept where dname = 'RESEARCH');


子查询语法:
select * from 主表 where 主键 = (select 外键 from 从表 where 主键='')


-- 找出工资为1500块的员工公司位置
select loc from dept where deptno=(select deptno from emp where sal=1500);


-- 分页查询:mysql :limit   sqlserver :top n
--          oracle :rownum伪列,先查询,后生成
--找出前1-5条 ,rownum只能查小,不能查大
select * from emp where rownum<=5;


-- 找出前3-5条,下面的办不到?因rownum>=3阻止查询出来,自然就没有了rownum
select * from emp where rownum>=3 and rownum<=5;


--先用子查询查询出来,然后再筛选t.*代表是已经查询出来的虚拟表8条数据
select * from
(
select rownum rn,t.* from (select * from emp order by deptno desc) t
where  rownum<=5
)
where rn>=3;
--------------------------------------------------------------------------------------------------
---分页查询
select * from emp where rownum>=5;
select rownum,emp.* from emp where rownum<=5;
select rownum,emp.* from emp order by deptno desc;
select * from emp order by deptno desc;
select rownum,t.* from (select * from emp order by deptno desc) t where rownum<=5;
select * from(select rownum rn,t.* from (select * from emp order by deptno desc) t where rownum<=5) where rn>=3;


---------------------------------------------------------------------------------------------------------------------------------------


-- select rowid from emp;
0 0
原创粉丝点击