SQL Server SELECT LAST N Rows

前端 未结 18 2129
猫巷女王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 03:56

    Here's something you can try without an order by but I think it requires that each row is unique. N is the number of rows you want, L is the number of rows in the table.

    select * from tbl_name except select top L-N * from tbl_name
    

    As noted before, which rows are returned is undefined.

    EDIT: this is actually dog slow. Of no value really.

提交回复
热议问题