本文介绍了从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文档

查询后使用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获取插入/更新行的计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 07:01
查看更多