SQL Server SELECT LAST N Rows

前端 未结 18 2217
猫巷女王i
猫巷女王i 2020-11-28 03:13

This is a known question but the best solution I\'ve found is something like:

SELECT TOP N *
FROM MyTable
ORDER BY Id DESC

I\'ve a table wi

18条回答
  •  眼角桃花
    2020-11-28 04:09

    If you want to select last numbers of rows from a table.

    Syntax will be like

     select * from table_name except select top 
     (numbers of rows - how many rows you want)* from table_name
    

    These statements work but differrent ways. thank you guys.

     select * from Products except select top (77-10) * from Products
    

    in this way you can get last 10 rows but order will show descnding way

    select top 10 * from products
     order by productId desc 
    

     select * from products
     where productid in (select top 10 productID from products)
     order by productID desc
    

     select * from products where productID not in 
     (select top((select COUNT(*) from products ) -10 )productID from products)
    

提交回复
热议问题