有什么方法可以区分 0 受影响的 MYSQL 更新行的情况吗?

2022-01-17 00:00:00 sql-update sql mysql

假设我有一个名为 't' 的表

Suppose I have a table named 't'

---------------
| key | value |
---------------
| 1   | abc   |
| 2   | def   |
---------------

考虑两个 MYSQL 查询

Consider two MYSQL queries

  1. UPDATE t SET value='abc' WHERE key=1
  2. UPDATE t SET value='abc' WHERE key=3

执行这两个查询也会使受影响的行"为 0(即不更新任何行),因为第一个查询是非更新更新,第二个查询是非匹配更新.

Executing both queries also give the 'affected rows' is 0 (That is, do NOT update any row) because first query is an non-updating update and second is an non-matching update.

有没有办法区分这两种情况?

Is there any way to distinguish these two cases?

推荐答案

如果你只想要'匹配'的行数(不再需要'改变'的行数),你可以设置 CLIENT_FOUND_ROWS 如此处所述:

if you only want the number of 'matched' rows (and no longer the number of 'changed' rows), you can set CLIENT_FOUND_ROWS as described here:

http://dev.mysql.com/doc/refman/5.5/en/mysql-affected-rows.html

对于 UPDATE 语句,默认情况下受影响的行值是实际更改的行数.如果您指定 CLIENT_FOUND_ROWS连接到 mysqld 时标记 mysql_real_connect(),受影响的行值是找到"的行数;也就是说,匹配WHERE 子句.

For UPDATE statements, the affected-rows value by default is the number of rows actually changed. If you specify the CLIENT_FOUND_ROWS flag to mysql_real_connect() when connecting to mysqld, the affected-rows value is the number of rows "found"; that is, matched by the WHERE clause.

相关文章