遇到个问题,例如select top 5 * from user order by age desc本意是想读取年龄最大的前5个用户,但是问题出现了,如果其中有两个年龄相同的话(应该是这个原因,具体没测试),这句话就会读取6条数据出来,我只需要5条数据,该怎么解决比较好呢?

解决方案 »

  1.   

    CREATE TABLE TBA
    (
    age  int,
    name nvarchar(50)
    )
    INSERT INTO TBA
    select '20','张三' union all
    select '21','里斯' union all
    select '31','王五' union all
    select '42','赵六' union all
    select '42','周武' union all
    select '39','杨八' union all
    select '52','李四' select top 5 * from  TBA order by age descage         name
    ----------- --------------------------------------------------
    52          李四
    42          周武
    42          赵六
    39          杨八
    31          王五(5 行受影响)
      

  2.   

    set rs=Server.CreateObject("ADODB.Recordset")
    rs.open "select top 5 * from link order by tin desc",conn,1,1
    if not rs.eof then
    for i=1 to rs.recordcount
    response.write rs("title")&"<br/>"
    rs.movenext
    next
    end if
    rs.close
    set rs=nothing
    我用的access+asp,确实会显示6条数据,麻烦你看下问题出在哪里?谢谢
      

  3.   

    不知道access行不行,你的问题主要是*号--CREATE TABLE TBA 
    --( 
    --age  int, 
    --name nvarchar(50) 
    --) 
    --INSERT INTO TBA 
    --select '20','张三' union all
    --select '21','里斯' union all
    --select '31','王五' union all
    --select '42','赵六' union all
    --select '42','周武' union all
    --select '39','杨八' union all
    --select '52','李四' 
     
    SELECT  *
    FROM    tba a
    WHERE   EXISTS ( SELECT 1
                     FROM   ( SELECT TOP 5
                                        age
                              FROM      tba
                            ) b
                     WHERE  a.age = b.age )