Answered by:
How to select previous id of the last inserted one?

Question
-
User-352524747 posted
How to select the previous id of the last inserted one?
Tuesday, May 14, 2013 5:03 AM
Answers
-
User1992938117 posted
As already you have added 4th row then you can not get 3rd row record directly, You have to do a query as
SELECT TOP 1 * From (select Top 2 * from Table_Movies ORDER BY ID DESC) x ORDER BY ID
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, May 14, 2013 8:55 AM -
User1992938117 posted
Yes there's no issue, i want to know what X means?x is an alias so first execute inner query (In this case you will get 2 rows which has highest id) then outer query will run top of 2 records and you will get 2nd highest value.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, May 14, 2013 10:25 AM
All replies
-
User-1333008299 posted
Hi,
db.GetLastInsertId();
HTH,
Jose.
Tuesday, May 14, 2013 5:22 AM -
User-352524747 posted
Not the last inserted id, but the previous of this,
lets say
db.GetLastInsertId(); -1
Tuesday, May 14, 2013 5:24 AM -
User-1333008299 posted
You should save it before the second insert, in order to access it.
Tuesday, May 14, 2013 5:29 AM -
User1992938117 posted
How to select the previous id of the last inserted one?SELECT * FROM <Yourtable> where id=LAST_INSERT_ID();
Tuesday, May 14, 2013 5:30 AM -
User895691971 posted
Why don't you create a new column, to save that ID everytime. Instead to create a whole new block of code!
Tuesday, May 14, 2013 7:52 AM -
User-352524747 posted
Afzaal.Ahmad.Zeeshan
Why don't you create a new column, to save that ID everytimeThe table has a column with ID.
Table_Movies
ID | Movie | Producers
1 | Title 1 | Name 1
2 | Title 2 | Name 2
3 | Title 3 | Name 3
4 | Title 4 | Name 4
I want to select the penultimate record, in this case ID = 3. And so on for more records.
Tuesday, May 14, 2013 8:26 AM -
User1992938117 posted
As already you have added 4th row then you can not get 3rd row record directly, You have to do a query as
SELECT TOP 1 * From (select Top 2 * from Table_Movies ORDER BY ID DESC) x ORDER BY ID
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, May 14, 2013 8:55 AM -
User-352524747 posted
Is x a SQL Alias?
SELECT TOP 1 * From (select Top 2 * from Table_Movies ORDER BY ID DESC) x ORDER BY ID
SELECT TOP 1 * From (select Top 2 * from Table_Movies ORDER BY ID DESC) AS T ORDER BY ID
Tuesday, May 14, 2013 9:29 AM -
User1992938117 posted
Is x a SQL Alias?I ran same query in SQL Server 2008R2 management studio and did not face any issue.
Tuesday, May 14, 2013 9:43 AM -
User-352524747 posted
Yes there's no issue, i want to know what X means?
Tuesday, May 14, 2013 10:03 AM -
User1992938117 posted
Yes there's no issue, i want to know what X means?x is an alias so first execute inner query (In this case you will get 2 rows which has highest id) then outer query will run top of 2 records and you will get 2nd highest value.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, May 14, 2013 10:25 AM