有这么一个场景,插入数据的时候,如果主键存在,则update;如果主键不存在,则insert。Mysql中提供了这样的用法:ON DUPLICATE KEY UPDATE。
执行的时候发现被影响的行数为2
With ON DUPLICATE KEY UPDATE, the affected-rows value per row is 1 if the row is inserted as a new row, 2 if an existing row is updated, and 0 if an existing row is set to its current values. If you specify the CLIENT_FOUND_ROWS flag to the mysql_real_connect() C API function when connecting to mysqld, the affected-rows value is 1 (not 0) if an existing row is set to its current values.
本文深入解析了Mysql中ON DUPLICATE KEY UPDATE语法的使用场景及影响行数的计数规则,当主键冲突时,该语法会更新已有记录,否则插入新记录,并详细解释了不同情况下的受影响行数。


被折叠的 条评论
为什么被折叠?



