美文网首页
mysql insert/update后,返回last_inse

mysql insert/update后,返回last_inse

作者: mingzhi618 | 来源:发表于2019-11-22 11:07 被阅读0次

1. 正常insert成功数据后,调用LAST_INSERT_ID函数即可获取插入的id

SELECT LAST_INSERT_ID();

2. update的时候,也可以通过以下方法获取被更新的数据id:

UPDATE table set id=LAST_INSERT_ID(id),  c=3 where name = 'bananer';

SELECT LAST_INSERT_ID();

3. 同理 insert on duplicate key update 也可以获取被插入或更新的id

INSERT INTO table (a,b,c) VALUES (1,2,3) ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id), c=3;

reference: https://stackoverflow.com/questions/778534/mysql-on-duplicate-key-last-insert-id

相关文章

网友评论

      本文标题:mysql insert/update后,返回last_inse

      本文链接:https://www.haomeiwen.com/subject/nvpfwctx.html