Getting all the fields from the row with the latest "ID"
I want to get the entire row of a table in SQL Server, but only the开发者_如何转开发 last inserted row (which is the row with the highest "ID"). I tried top, max etc, but can't seem to get this to work. So if the highest ID is 15, I want to get all the fields of that row (15).
Any ideas?
Thanks
Perhaps try this?
SELECT * FROM MyTable
WHERE ID = (SELECT MAX(ID) FROM MyTable)
OR
SELECT TOP 1 * FROM MyTable
ORDER BY ID DESC
SELECT * from Table1
WHERE
ID = ( SELECT MAX(ID) FROM Table1)
SELECT *
FROM table
WHERE id = (
SELECT MAX(id)
FROM table
);
This should do it ...
select top 1 *
from yourtable
order by id desc
精彩评论