gpt4 book ai didi

mysql - 获取 INSERT ... SELECT ... ON DUPLICATE KEY UPDATE 的 "Records"和 "Duplicates"计数

转载 作者:可可西里 更新时间:2023-11-01 07:07:41 24 4
gpt4 key购买 nike

INSERT ... SELECT ... ON DUPLICATE KEY UPDATE返回作为 affected-rows(inserted count) + (updated count) * 2 派生的数字,这是 well documentedmultiple places .

但是在 MySQL Command-Line Tool 的输出中,我注意到了这个额外的信息:

> INSERT INTO ...
-> SELECT ... FROM ...
-> ON DUPLICATE KEY UPDATE ...
-> ;
Query OK, 97 rows affected (0.03 sec)
Records: 2425 Duplicates: 28 Warnings: 0

即,数字 Records:Duplicates:

分析确定:

  • 97 行受影响affected-rows(又名 ROW_COUNT())。
  • Records: 2425SELECT 部分获取的行数。
  • Duplicates: 28ON DUPLICATE KEY UPDATE 部分实际更改的行数。

因此:

  • affected-rows - Duplicates * 2 是实际插入的行数。
  • Records - affected-rows - Duplicates 是重复但未更改的行数(即值设置为相同的值)。

这给我们带来了一个问题:如何在程序中获取这些数字 RecordsDuplicates(我使用的是 MySQL Connector/J 如果这有助于回答问题。)

可能用于 Records:,在 FOUND_ROWS() 上发出 SELECT直接在 INSERT ... SELECT ... ON DUPLICATE KEY UPDATE 之后是一种方法。

我不知道 Duplicates: 从何而来。

最佳答案

C api 不提供对这些值(或计算这些值的基础信息)作为数字的直接访问,就像它对 mysql_affected_rows() 所做的那样。 .

但是您可以使用 mysql_info() 访问该消息:

mysql_info()

const char *mysql_info(MYSQL *mysql)

Description

Retrieves a string providing information about the most recently executed statement, but only for the statements listed here. For other statements, mysql_info() returns NULL. The format of the string varies depending on the type of statement, as described here. The numbers are illustrative only; the string contains values appropriate for the statement.

  • INSERT INTO ... SELECT ...

    String format: Records: 100 Duplicates: 0 Warnings: 0

  • [...]

  • UPDATE

    String format: Rows matched: 40 Changed: 40 Warnings: 0

Return Values

A character string representing additional information about the most recently executed statement. NULL if no information is available for the statement.

如果您需要详细访问这些值,您可以/必须解析这些(依赖于查询的)字符串。 mysql 客户端仅按原样显示此消​​息。

不幸的是,并非每个 API(包括 MySQL Connector/J)都实现或中继此功能,因此这些详细值似乎无法在此处访问。

关于mysql - 获取 INSERT ... SELECT ... ON DUPLICATE KEY UPDATE 的 "Records"和 "Duplicates"计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52731928/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com