Any way to distinguish the cases of 0 affected row of MYSQL update?

天大地大妈咪最大 提交于 2020-07-07 02:54:29

问题


Suppose I have a table named 't'

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

Consider two MYSQL queries

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

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?


回答1:


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

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.



来源:https://stackoverflow.com/questions/17398301/any-way-to-distinguish-the-cases-of-0-affected-row-of-mysql-update

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!