取前十条取后十条取区间值-限制取数区间

MySQL:
select * from table1 where 1=1 limit 10;

SQL Server:
读取前10条:select top (10) * from table1 where 1=1;
读取后10条:select top (10) * from table1 order by id desc;
在SQL Server里面,如何读取按照某个排序,第3到6这四个记录
select top 4 * from table1 where id not in(select top 2 id from table1);

oracle:
读取前10条:select * from table1 where rownum<=10;
读取后10条:select * from table1 where rownum<=10 order by id desc;
--取出第三条到第六条数据
(效率不高)
select * from (select * from table1 where rownum<=3) minus (select * from table1 where rownum<3);
--或者下面这个
select * from (select * from (select rownum rn, a.* from table1 a) where rn>=3) where rn<=6;

where 1=1仔细揣摩一下,在这里表示2种意思。
第一,当然它毫无疑问是一个恒等式,在这里有没有都没关系。不禁联想起SQL注入的黑客手法。
第二,这里写个1=1,它的重点是想告诉我,这里可以写条件,比如:

格式:select * from 表名 where 列名=' ' limit 5,10;  -- limit numStart, num,其中numStart默认为0,表示从第1条开始,可以省略,
-- 条数下标从0开始,numStart表示从numStart+1条开始,num表示取num条
例子:select * from tb_email where toname='Jef' limit 5,10;
翻译:表示在tb_email表中的发送人为Jef的所有数据从第6条开始取10条数据。

另外 desc是descend降序意思 asc是ascend 升序的意思
select top (10) * from table1; 默认为asc,并且默认用主键排序
select top (10) * from table1 order by id desc为desc;
想要加条件的话,即:
select top (10) * from table1 where shijian=' ' order by id desc;

这时不禁想问,用'select * from table order by'显示时怎么让order by 后面同时满足两个条件?
答:select * from table order by shijina,paixu desc;
在实际项目中,我建议大家做一个按钮:
select case orderby
case 'shijian'
sql="select * from my_table order by shijian desc"
case 'paixu'
sql="select * from my_table order by paixu desc"
case else
sql="select * from my_table order by shijina,paixu desc"
end select
/
原文地址:https://www.cnblogs.com/tufujie/p/4928473.html