如何从数据库中获取最后一条记录 [英] how can fetch last record from the database
本文介绍了如何从数据库中获取最后一条记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想要数据库的最后一行,所以我应该为此写哪个查询?
i want last row from the database so which query should i write for that?
推荐答案
获取SQL数据库中的最后一条记录:
选择*
从表
哪里ID =(从表中选择MAX(ID))
或
按ID desc从TABLE_NAME顺序中选择前1个*
GET last record in a SQL database:
SELECT *
FROM TABLE
WHERE ID = (SELECT MAX(ID) FROM TABLE)
or
select top 1 * from TABLE_NAME order by ID desc
Sumit,
这取决于您的数据库类型.
如果您的表中有一个具有自动递增功能的主键,请使用以下查询.
Hi Sumit,
This is depend on your database type.
If you have a primary ke in your table with auto increment then use below query.
Select * From tableName Where PK = (Select Max(PK) From tableName)
如果您使用的是MySql,请使用以下查询.
If you are using MySql then use below query.
SELECT * FROM tableName WHERE id = LAST_INSERT_ID( );
如果您使用的是ORALCE,请使用以下查询.
If you are using ORALCE then use below query.
SELECT * FROM tableName where ROWID = (SELECT MAX(ROWID) FROM tableName);
希望对您有所帮助.
Hope this will help you.
Hello Sumit,
您必须使用
Hello Sumit,
you have to use
SELECT SCOPE_IDENTITY() in sql server
SCOPE_IDENTITY returns the last inserted IDENTITY value in this current scope.
这篇关于如何从数据库中获取最后一条记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文