首页 > 其他 > 详细

取前十条取后十条取区间值(常见)

时间:2015-11-01 21:07:25      阅读:515      评论:0      收藏:0      [点我收藏+]
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;
例子:select * from tb_email where toname=caixiangyu limit 5,10;
翻译:表示在tb_email表中的发送人为caixiangyu的所有数据从第5个开始取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
/

 

取前十条取后十条取区间值(常见)

原文:http://www.cnblogs.com/tufujie/p/4928473.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!