LAST_INSERT_ID() MySQL

后端 未结 11 2403
花落未央
花落未央 2020-11-22 10:56

I have a MySQL question that I think must be quite easy. I need to return the LAST INSERTED ID from table1 when I run the following MySql query:

INSERT INTO          


        
11条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-22 10:59

    Just to add for Rodrigo post, instead of LAST_INSERT_ID() in query you can use SELECT MAX(id) FROM table1;, but you must use (),

    INSERT INTO table1 (title,userid) VALUES ('test', 1)
    INSERT INTO table2 (parentid,otherid,userid) VALUES ( (SELECT MAX(id) FROM table1), 4, 1)
    

提交回复
热议问题