• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

“取出数据表中第10条到第20条记录”的sql语句+selecttop用法

mysql 搞代码 4年前 (2022-01-09) 24次浏览 已收录 0个评论

1.首先,select top用法: 参考问题 select top n * from和select * from的区别 select * from table — 取所有数据,返回无序集合 select top n * from table — 根据表内数据存储顺序取前n条,返回无序集合 select * from table order by id desc — 取所

1.首先,select top用法:

参考问题 select top n * from和select * from的区别

select * from table — 取所有数据,返回无序集合

select top n * from table — 根据表内数据存储顺序取前n条,返回无序集合

select * from table order by id desc — 取所有数据,按id逆序返回有序列表

select top n * from table order by id desc– 先按id逆序,再取前n条,返回按id排序的有序集合【注意,按某个属性排序,该排序属性的数据列值最好是不重复的,如果有重复的,那排序属性值相等的这些行在结果集中的顺序事先是不能确定的】

栗子如下~

我们以pid作为排序属性值,第16行,第19行和第20行的pid值相等。现在取以pid排序的倒数5条记录:

Connection con=new SQLConnection().getConnection();String sql="select top 5 * from test order by pid desc";System.out.println("select begins...");Statement statement=con.createStatement();ResultSet result = statement.executeQuery(sql);while (result.next()) {System.out.println(result.getInt(1)+","+result.getString(2)+","+result.getString(3));}System.out.println("select ends...");con.close();statement.close();result.close();con=null;statement=null;result=null;

结果:

select begins...3,as,916,tt,8 【三者顺序事先不能确定】19,gh,820,jk,86,bb,7select ends...

2. 类似于“查询第10条到第20条记录”的sql语句写法 === 常应用于分页显示上
1) String sql=”select top 10 * from (select * from test where id<21) m order by m.id desc"; //注意id为主键,子查询取出前20条记录,主查询先降序再取前10条,但结果是降序的,所以后续处理时要注意
2)查询第m条到第n条记录:
String sql=”select top n-m+1 * from test where (id not in(select top m-1 id from test))”; //可以是正常顺序的第m条到第n本文来源gao@daima#com搞(%代@#码网@条记录写法,很推荐哦~
3)【有些小毛病,我自己也不知道错在哪了,写出来,若有某位看客知道烦请留言一下哦~】 String sql = “select top 10 * from (select top 20 * from test) a order by a.id desc”; 以上述表中数据试了一下,结果是:【为什么是从第12条到第21条嘞?想不明白】 21,kl,100
20,jk,8
19,gh,8
18,aas,18
17,qw,19
16,tt,8
15,ww,15
14,hh,13
13,gg,16
12,ui,11

关于3)的疑惑,在博客园找到这样一处文章《来谈谈SQL数据库中”简单的”SELECT TOP—可能有你从未注意到的细节》

——————————-引用开始———————————–

数据表如下:

ID  EMPNO  NAME  AGE  

1   26929   Jerome   28
2   28394   Quince  27
3   20983   Green   30
4   27189   Mike    30
5   23167   Arishy   30
6   26371   Yager   29

我写了SQL语句想取得第3、4笔数据,测试分页玩的。

select top 2 * from (select top 4 * from Member ) m order by m.RowID desc

我执行中间那一段子查询:select top 4 * from Member

取得的是:

1   26929   Jerome   28
2   28394   Quince  27
3   20983   Green   30
4   27189   Mike    30

但是整个SQL语句的结果却是:【确实遇到过这样的问题,但是不知道原因….】

5   23167   Arishy   30
6   26371   Yager    29

select top 2 * from (select top 4 * from table) m order by m.id desc —– 扫描完table后先降序然后再在4行中取2行 【有点疑问,不是扫描完table–取4行–降序–取2行么??】

select top 2 * from (select top 4 * from table order by id asc) m order by m.id desc —– 扫描完table后先升序取4行然后再把这4行降序取2行

问题涉及到SQL中的子查询:

出现在from子句中的表我们称为派生表。派生表是虚拟的,未被物理具体化,也就是说当编译

的时候,如(select top 2 * from (select top 4 * from table) m order by m.id

desc ),外部查询和内部查询会被合并,并生成一个计划。

(注意事项:在派生表里面一般不允许使用order by除非指定了top,也就是说select top

2 * from (select * from zhuisuo order by id asc) m order by m.id desc这句语句是不

能执行的)。派生表是个虚拟表要被外部引用,而order by返回的不是表而是游标.所以只用order by的话是被限制的。然而为什么使用top加order by又可以了?是因为top可以从order by返回的游标里选择指定数量生成一个表并返回。

再举例关于top需要注意的细节

1、使用top返回随机行,很多人会想到用RAND函数从而得到这样一个语句

select top 4 id,name from table order by rand();

经过多次查询后,你会失望的发现它没有返回随机行。这是因为每个查询只调用它一次而不是每

行调用它一次。

2、注意insert中使用top,正确的倒叙插入top方法应该是:

insert into table

select top (4) * from table order by id desc


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:“取出数据表中第10条到第20条记录”的sql语句+selecttop用法

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址