zoukankan      html  css  js  c++  java
  • “取出数据表中第10条到第20条记录”的sql语句+select top 使用方法

    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,9
    16,tt,8  【三者顺序事先不能确定】
    19,gh,8
    20,jk,8

    6,bb,7
    select 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条记录写法。非常推荐哦~

    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

    ------------------------------引用结束----------------------------

    具体见原博客,关于top的细节,还是没有搞明确呢,往后再多看看-多实践再总结咯



  • 相关阅读:
    LVS基于DR模式负载均衡的配置
    Linux源码安装mysql 5.6.12 (cmake编译)
    HOSt ip is not allowed to connect to this MySql server
    zoj 3229 Shoot the Bullet(无源汇上下界最大流)
    hdu 3987 Harry Potter and the Forbidden Forest 求割边最少的最小割
    poj 2391 Ombrophobic Bovines(最大流+floyd+二分)
    URAL 1430 Crime and Punishment
    hdu 2048 神、上帝以及老天爷(错排)
    hdu 3367 Pseudoforest(最大生成树)
    FOJ 1683 纪念SlingShot(矩阵快速幂)
  • 原文地址:https://www.cnblogs.com/jhcelue/p/6882891.html
Copyright © 2011-2022 走看看