How to delete old entries based on MAX(id) after an INSERT query? How can I chain multiple sql queries and reuse a query result as a variable?

Specific: I want to store MAX(id), persist new data, and delete the old one based on the maxid fetched before.

SELECT MAX(id) as maxid FROM table; //TODO how to save this?
INSERT INTO table SELECT * from temp_table...
DELELTE FROM table where id &lt; maxid; //TODO how to reuse?
CREATE TABLE temp LIKE real;
load up `temp`
copy 'last' from real into temp  (why??)
RENAME TABLE real TO old,
             temp TO real;
DROP TABLE old;


Depending on the size of the table, this could be a lot faster than using DELETE.  Also, the table is never not there.

No 'transaction' is needed.