In SQL Server 2008 and higher what is the best/safest/most correct way
To get the last record, the following is the query. mysql> select *from getLastRecord ORDER BY id DESC LIMIT 1; The following is the output. The above output shows that we have fetched the last record, with Id 4 and Name Carol.
How to get last inserted id of a MySQL table using LAST_INSERT_ID() We will be using the LAST_INSERT_ID() function to get the last inserted id. Last_insert_id() MySQL function returns the BIG UNSIGNED value for an insert statement on an auto_increment column.
The following is the syntax to get the last 10 records from the table. Here, we have used LIMIT clause. SELECT * FROM ( SELECT * FROM yourTableName ORDER BY id DESC LIMIT 10 )Var1 ORDER BY id ASC; Let us now implement the above query.
The LAST() function in Structured Query Language shows the last value from the specified column of the table.
SELECT IDENT_CURRENT('Table')
You can use one of these examples:
SELECT * FROM Table WHERE ID = ( SELECT IDENT_CURRENT('Table')) SELECT * FROM Table WHERE ID = ( SELECT MAX(ID) FROM Table) SELECT TOP 1 * FROM Table ORDER BY ID DESC
But the first one will be more efficient because no index scan is needed (if you have index on Id column).
The second one solution is equivalent to the third (both of them need to scan table to get max id).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With