REPLACE INTO 为什么返回”2 rows affected”

互联网,请记住我 2009-10-16 12:13:22 累计浏览 3,569 次

    表的结构:

     desc urls;

    +――-+―――――――+――+―-+―――+――-+

    | Field | Type | Null | Key | Default | Extra |

    +――-+―――――――+――+―-+―――+――-+

    | id | bigint(16) unsigned | NO | PRI | NULL | |

    | url | varchar(255) | NO | | NULL | |

    | md516 | varchar(32) | NO | UNI | | |

    +――-+―――――――+――+―-+―――+――-+

    3 rows in set (0.01 sec)

    原有数据:

    +―-+―――――――-+――――――+

    | id | url | md516 |

    +―-+―――――――-+――――――+

    | 0 | http://www.162cm.com/ | 49f6f2aaa26c124a |

    +―-+―――――――-+――――――+

    1 row in set (0.00 sec)

    执行的语句:

    REPLACE INTO urls (`url`,`md516`,`id`) VALUES (’http://www.162cm.com/’,\'49f6f2aaa26c124a’,0);

    而执行这条语句的返回是:

    Query OK, 2 rows affected (0.00 sec)

    为啥呢….看起来数据啥也没变,不是吗?

    看文档:http://au2.php.net/mysql_affected_rows:

     Returns the number of affected rows on success, and -1 if the last query failed.

    If the last query was a DELETE query with no WHERE clause, all of the records will have been deleted from the table but this function will return zero with MySQL versions prior to 4.1.2.

    When using UPDATE, MySQL will not update columns where the new value is the same as the old value. This creates the possibility that mysql_affected_rows() may not actually equal the number of rows matched, only the number of rows that were literally affected by the query.

    The REPLACE statement first deletes the record with the same primary key and then inserts the new record. This function returns the number of deleted records plus the number of inserted records.

同分类推荐文章

  1. 国产数据库迁移:真正花时间的从来不是搬数据 (2026-11-14 04:10:02)
  2. 由于con$.con#值达到极限,触发数据库增加约束报ORA-600 12807错误 (2026-11-14 03:40:02)
  3. MongoDB 离线提取工具—mongoext (2026-11-13 18:10:02)

查看更多 数据库 文章 →

建议继续学习

  1. 用Hyer来进行网站的抓取 (累计阅读 158,836)
  2. MySQL数据库在实际应用一些方面的介绍 (累计阅读 36,809)
  3. WordPress插件开发 -- 在插件使用数据库存储数据 (累计阅读 29,568)
  4. Mysql监控指南 (累计阅读 21,830)
  5. 由浅入深探究mysql索引结构原理、性能分析与优化 (累计阅读 17,051)
  6. 如何查找消耗资源较大的SQL (累计阅读 15,549)
  7. 在Apache2.2.XX下安装Mod-myvhost模块 (累计阅读 13,481)
  8. 15个最好的免费开源电子商务平台 (累计阅读 12,949)
  9. 浅谈MySQL索引背后的数据结构及算法 (累计阅读 12,691)
  10. 整理了一份招PHP高级工程师的面试题 (累计阅读 12,221)