获取前10条和第10到20条数据sql

oracle:

rownum 是把SQL出来的结果进行编号,始终从1开始,常见的用途就是用来分页输出。

select * from ts_user where rownum < 11;
select * from (select * from ts_user order by id desc ) where rownum<=10;

把rownum转成实例,因为rownum本身只能用<=的比较方式,只有转成实列,这样就可做 >=的比较了。

select *  from 
(select a.*, rownum rn from ts_user a)
where rn >= 10 and rn <= 20

row_number() over()
over里头的分组及排序的执行晚于“where,group by,order by”的执行。

select *  from (
select account,user_name,**row_number() over( order by create_time) rs** from ts_user
)where rs <11

select *  from (
	select gender,user_name,create_time,**row_number() over(partition by gender order by create_time) rs** from ts_user
)where rs >= 10 and rs <= 20

mysql:
索引从0开始,

 select * from ts_dict_entry order by id desc limit 0,10

等同于

 select * from ts_dict_entry order by id desc limit 10

从10开始,取11条

select * from ts_dict_entry order by id desc limit 9,11

分组后排序

select * from ts_dict_entry a 
where 2>(select count(*) from ts_dict_entry where type_id=a.type_id and sort_no

sqlserver:

select top 10  * from ts_user order by id desc

子查询结果要取别名

select *  from (
select account,user_name,row_number() over( order by create_time) rs from ts_user
) t where t.rs <11

子查询结果要取别名

select *  from (
	select gender,user_name,create_time,row_number() over(partition by gender order by create_time) rs from ts_user
)t where  rs >= 10 and rs <= 20

select *  from (
select account,user_name,row_number() over( order by create_time) rs from ts_user
) t where rs between 10 and 20

postgresql:

从第3条开始,取10条

 select * from ts_dict_entry order by id desc limit 10 offset 2

分组查询,与mysql一致

select * from ts_dict_entry a 
where 2>(select count(*) from ts_dict_entry where type_id=a.type_id and sort_no

你可能感兴趣的:(获取前10条和第10到20条数据sql)