插入后获取mysql的最后一个插入ID [英] Getting last insert id for mysql after Insert
本文介绍了插入后获取mysql的最后一个插入ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的查询:
INSERT INTO table1 (field1,field2) VALUES ('$value1','$value2') ON DUPLICATE KEY UPDATE field1 = '$value1'
如果要插入,我想获取最后一个插入ID,该怎么办?如果查询最终进行了更新,我不希望最后一个插入ID.有没有办法确定是进行更新还是插入?
I then want to get the last insert id if it does the insert, how can I do this? If the query ends up doing an update I dont want the last insert id. Is there a way to determine if it did an update or a insert?
推荐答案
我想我应该在发布之前搜索该网站.基本上添加此工作:
I guess I should of searched the site before posting. Basically adding this worked:
id=LAST_INSERT_ID(id)
在重复更新中.我在这里找到了答案:
On the on duplicate update. I found that answer here:
这篇关于插入后获取mysql的最后一个插入ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文