从ON DUPLICATE KEY UPDATE获取插入/更新行的计数 [英] Getting count of insert/update rows from ON DUPLICATE KEY UPDATE

查看:548
本文介绍了从ON DUPLICATE KEY UPDATE获取插入/更新行的计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一条语句试图插入一条记录,如果该记录已经存在,它只会更新记录.

I have a statement that tries to insert a record and if it already exists, it simply updates the record.

INSERT INTO temptable (col1,col2,col3)
VALUES (1,2,3)
ON DUPLICATE KEY UPDATE col1=VALUES(col1), col2=VALUES(col2), col3=VALUES(col3);

完整的语句有多个插入,我想根据UPDATE计算INSERT的数量.我可以使用MySQL变量来执行此操作吗,搜索后还没有找到一种方法.

The full statement has multiple inserts and I'm looking to count number of INSERTs against the UPDATEs. Can I do this with MySQL variables, I've yet to find a way to do this after searching.

推荐答案

来自 MySQL文档

对于"INSERT ... ON DUPLICATE KEY UPDATE"查询,如果执行插入操作,则返回值为1;对于现有行的更新,返回值为2.

In the case of "INSERT ... ON DUPLICATE KEY UPDATE" queries, the return value will be 1 if an insert was performed, or 2 for an update of an existing row.

查询后使用mysql_affected_rows(),如果执行了INSERT,将为您提供1,如果执行了UPDATE,则将为您提供2.

Use mysql_affected_rows() after your query, if INSERT was performed it will give you 1 and if UPDATE was performed it will give you 2.

这篇关于从ON DUPLICATE KEY UPDATE获取插入/更新行的计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆