IT技术博客大学习 共学习 共进步
全部 移动开发 后端 数据库 AI 算法 安全 DevOps 前端 设计 开发者

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

互联网,请记住我 2009-10-16 12:13:22 累计浏览 3,396 次
本机暂存

    表的结构:

     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. 使用deepseek进行Oracle恢复,引起重大故障 (2026-06-22 10:56:00)
  2. 接手一个只差临门一脚的数据库恢复 (2026-06-18 00:13:09)
  3. 我做了一个 AI 版的 StarRocks 升级风险扫描工具,直接帮我定位到一个风险 (2026-06-15 01:00:00)

查看更多 数据库 文章 →

建议继续学习

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