How do I select last 5 rows in a table without sorting?

前端 未结 22 1326
-上瘾入骨i
-上瘾入骨i 2020-11-30 04:37

I want to select the last 5 records from a table in SQL Server without arranging the table in ascending or descending order.

22条回答
  •  醉酒成梦
    2020-11-30 05:09

    In SQL Server 2012 you can do this :

    Declare @Count1 int ;
    
    Select @Count1 = Count(*)
    FROM    [Log] AS L
    
    SELECT  
       *
    FROM    [Log] AS L
    ORDER BY L.id
    OFFSET @Count - 5 ROWS
    FETCH NEXT 5 ROWS ONLY;
    

提交回复
热议问题