SQL - How to find the highest number in a column?

前端 未结 15 1029
借酒劲吻你
借酒劲吻你 2020-12-30 18:25

Let\'s say I have the following data in the Customers table: (nothing more)

ID   FirstName   LastName
-------------------------------
20   John        Macken         


        
15条回答
  •  一向
    一向 (楼主)
    2020-12-30 19:24

    If you're talking MS SQL, here's the most efficient way. This retrieves the current identity seed from a table based on whatever column is the identity.

    select IDENT_CURRENT('TableName') as LastIdentity
    

    Using MAX(id) is more generic, but for example I have an table with 400 million rows that takes 2 minutes to get the MAX(id). IDENT_CURRENT is nearly instantaneous...

提交回复
热议问题