sql 应用大全(分页、内连接、左右连接、全连接、交叉连接)

》分页查询6-10的数据:

select * from

(select rownum rn,t.* from(select id,title,content,occurtime from news order by occurtime desc) t  where rn<=10)

where rn>=6; 

》连接查询:

bookhttps://i-blog.csdnimg.cn/blog_migrate/729eb61509340df93cd20d5d2caf4d0d.jpegstudent https://i-blog.csdnimg.cn/blog_migrate/8f47f3a202ce5f4dc45cc3336ca91688.jpeg

内连接:

Select * from book as b  join student as s ON b.StudentId=s.StudentId

https://i-blog.csdnimg.cn/blog_migrate/277a1277fc5632360a9784531859b86a.jpeg

左连接:

select * from book as b left join student as s ON b.StudentId=s.StudentId

select * from book as b ,student as s where b.studentid(+)=s.studentid

https://i-blog.csdnimg.cn/blog_migrate/8b149642e5737a9a23dc2df877181774.jpeg

右连接:

select * from book as b right join student as s ON b.StudentId=s.StudentId

select * from book as b ,student as s where b.studentid=s.studentid(+)

https://i-blog.csdnimg.cn/blog_migrate/a3efe00d3d126992e07041ebae7a3457.jpeg

全连接:

select * from book as b full outer join student as s ON b.StudentId=s.StudentId

https://i-blog.csdnimg.cn/blog_migrate/660af1bfe73dba32ef4eb3d21783c1ee.jpeg

交叉连接:

select * from book  as b CROSS Join student  as a Order by b.BookId

https://i-blog.csdnimg.cn/blog_migrate/11364ee9cbfa0fe73716d5fec75dbc9a.jpeg

union和union all的区别是,union会自动压缩多个结果集合中的重复结果,而union all则将所有的结果全部显示出来,不管是不是重复

union:

select ename,job,sal from xcr_emp where job='MANAGER'

union

select ename,job,sal from xcr_emp where sal>2500;

union all:

select ename,job,sal from xcr_emp where job='MANAGER'

union all

select ename,job,sal from xcr_emp where sal>2500;

over():开窗函数,Oracle从8.1.6开始提供分析函数,分析函数用于计算基于组的某种聚合值,它和聚合函数的不同之处是:对于每个组返回多行,而聚合函数对于每个组只返回一行

rank()和dense_rank()可以将所有的都查找出来,rank可以将并列第一名的都查找出来;rank()和dense_rank()区别:rank()是跳跃排序,有两个第二名时接下来就是第四名。

row_number()该函数可以根据指定的字段分组,再按照指定的字段排序,然后生成组内连续且唯一的数字。

select ename,sal,deptno,row_number() over(partition by deptno order by sal desc)chen from xcr_emp;

select ename,sal,deptno,rank() over(partition by deptno order by sal desc)chen from xcr_emp;

连续但唯一

select ename,sal,deptno,dense_rank() over(partition by deptno order by sal desc)chen from xcr_emp;

连续但不唯一

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值