Donatas Veikutis
Donatas Veikutis

Reputation: 1004

How to select last inserted row by id?

that my problem:

I have database table like that:

id (AI)    market_id
1             6       
2            10      
3             6       
4            10   
5            11     

How to select last inserted market_id by id?

Upvotes: 0

Views: 3686

Answers (3)

dexametason
dexametason

Reputation: 1133

You can use LAST_INSERT_ID():

SELECT market_id FROM <table> where id=LAST_INSERT_ID();

Upvotes: 1

Russell Hart
Russell Hart

Reputation: 1851

For SQL Server,

Select TOP 1 ID, market_I'd from table order by ID desc 

Upvotes: 0

Cyclonecode
Cyclonecode

Reputation: 30071

You could do something like this:

SELECT * FROM <your_table> ORDER BY id DESC LIMIT 1;

If you only are interested in the market_id you could change the * in the above query to market_id.

Upvotes: 3

Related Questions