如何删除/选择表中的最后n行而不进行排序 [英] how to delete /select last n rows in the table without sorting

查看:103
本文介绍了如何删除/选择表中的最后n行而不进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好专家,

如何从mytable中获取最后n条记录.
对此的任何查询,请立即通知

hello experts,

how to fetch last n records from mytable.
any query for that,please inform immediatle

推荐答案

假设ID是一个身份列,我会做这样的事情:
Assuming ID is an identity column, I would do something like this:
SELECT TOP 10 
[ID] ,[Name]
  FROM [dbo].[myTable]
  ORDER BY ID DESC



当您要最后一个"时,您要强加命令-如果不对结果进行排序,整个事情将毫无意义.

最好的问候
Espen Harlinn



As you are asking for "last" you are imposing order - the whole thing would be meaningless without ordering the result.

Best regards
Espen Harlinn


解决方案2很好............
Solution 2 is fine............


要从mytable中获取最后n条记录
To fetch last n records from mytable
Declare @n int
Set @n=5

select * from mytable
         where mytableid not in (
                                   select top ((select count(*) 
                                    from mytable) - @n ) mytableid
        from mytable


这篇关于如何删除/选择表中的最后n行而不进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆