Possible Duplicate:
SELECT … FOR UPDATE and MAX()
Which rows in this query lock ?
select max(id) from table where id like '9%' for update
what occur if another user run this query ?
this is related question.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you have no index on id, this will lock all of the records. But I guess you have such index. So this will lock all records that are matching, including some records in between (if you are locking 3 and 5, 4 is also being locked)
edit
In the case of SELECT max(id) FROM … you don’t need to read any rows from the table, because this information can be optained from the index. If you want to lock exactly one row, the correct query would be SELECT * FROM table WHERE id = SELECT max(id) FROM table