I have the following simple two-selects sequence within a stored procedure:
SELECT * FROM mytable WHERE <conditions>;
SELECT id FROM mytable ORDER BY id DESC LIMIT 1;
where id is the primary key of mytable.
The overall purpose is to select some rows from mytable (including the newest ones - ie the ones with the heighest id values) and then, with the second select, get the id of the last/newest row in the same table.
Question: how do I ensure that no new records gets inserted between the first and the second select?
I was going to use:
TRANSACTION
select <1..> FOR UPDATE;
select <2..>;
COMMIT
but will that always work and is there a better way?
Or can do something different to achieve the goal of getting the heighest id value from the first select without having the second select?