How to select top N from a table

后端 未结 9 807
醉酒成梦
醉酒成梦 2020-12-11 21:18

I have to select the top 25 records from a table according to the column Num.

There are two issues. First, the table is not sorted by Num.

相关标签:
9条回答
  • 2020-12-11 21:21

    For SQL Server:

    select top 25 * from table order by Num asc
    

    For mySQL:

    select * from table order by Num asc limit 25
    
    0 讨论(0)
  • 2020-12-11 21:21

    In Firebird,

    select first 25 
    from your_table
    order by whatever
    
    0 讨论(0)
  • 2020-12-11 21:22
    select top 25 *
    from your_table
    order by Num asc
    

    On SQL Server that would select the 25 first records starting from the lowest value of Num. If you need the highest, use "desc" instead of "asc".

    0 讨论(0)
  • Not sure I understand the requirement, but you can do:

    SELECT TOP 25 Num FROM Blah WHERE Num = 'MyCondition'
    

    If there aren't 25 records, you won't get 25. You can perform an ORDER BY and the TOP will listen to that.

    0 讨论(0)
  • 2020-12-11 21:31

    Oracle:

    Select *
    FROM Table
    WHERE rownum <= 25
    

    MSSQL:

    SELECT TOP 25 * 
    from Table
    

    Mysql:

    SELECT * FROM table
    LIMIT 25
    
    0 讨论(0)
  • 2020-12-11 21:37

    Depending on the database implementation you're using, it could be using the limit statement (as one other answer suggests) or it could be like this:

    SELECT TOP 25 Num, blah, blah ...
    
    0 讨论(0)
提交回复
热议问题