Alex Rivera | Logout

Is it conceptually right to do a SELECT MAX(id) etc. for finding the last inserted row?

Asked 2011-12-01T17:09:24.477
12

I was reviewing a Drupal module when I found this pattern for getting the id of the row last inserted:

SELECT MAX(id) FROM ... WHERE ...

where the id is a field working in a usual autoincrement way.

Is this conceptually right to do? Is there any situation when this pattern will fail in a MySQL/PostgreSQL environment?

Edit:

Thanks for the excellent comments and answers!

I should clarify my question: I meant a situation where someone would like to find out the id of the last inserted row regardless of the session.

Edit
Report

1 Answer

2

In MySQL you can simply use last_insert_id () which is more guaranteed to work.

answered 2011-12-01T17:12:49.140

Your Answer