Getting last record from mysql

Almost done. You succeed in getting the insert order. So: select myId, @rowid:=@rowid+1 as myrow from maxID, (SELECT @rowid:=0) as init ORDER BY myrow desc LIMIT 1; In my console I get the following: mysql> select myId, @rowid:=@rowid+1 as myrow from maxID, (SELECT @rowid:=0) as init ORDER BY myrow desc LIMIT 1; +——+——-+ | myId … Read more

LAST_INSERT_ID() MySQL

You could store the last insert id in a variable : INSERT INTO table1 (title,userid) VALUES (‘test’, 1); SET @last_id_in_table1 = LAST_INSERT_ID(); INSERT INTO table2 (parentid,otherid,userid) VALUES (@last_id_in_table1, 4, 1); Or get the max id from table1 (EDIT: Warning. See note in comments from Rob Starling about possible errors from race conditions when using the … Read more