通过更新已存在的字段的值来获取受影响的行值

时间:2021-01-21 23:10:20

I've got a table

我有一张桌子

ID   |   NAME    |  VALUE  |
----------------------------
 1   |   Test1   |  VALUE1 |
 2   |   Test2   |  VALUE2 |
 3   |   Test3   |  VALUE3 |
 4   |   Test4   |  VALUE4 |
 5   |   Test5   |  VALUE5 |

I'm running this query dynamically

我正在动态运行此查询

$query="UPDATE tables SET `VALUE`='VALUE1' WHERE `ID`='1'"

Here i'm updating a value which is already stored. This doesn't update the field value. Because of this i can't get affected rows. I'm using PHP

这里我正在更新已存储的值。这不会更新字段值。因为这个我不能受到影响的行。我正在使用PHP

Is there any way to get the affected rows with the above query.

有没有办法通过上面的查询获取受影响的行。

2 个解决方案

#1


Your query works according to this SQL Fiddle:

您的查询根据此SQL Fiddle工作:

http://sqlfiddle.com/#!9/84beb/1

#2


If you set a field to the value it already has, it's not counted in "affected rows". If you want to know how many rows might have been modified based on the WHERE clause, you need to do a separate SELECT with the same clause:

如果将字段设置为已有的值,则不会将其计入“受影响的行”。如果您想知道基于WHERE子句可能修改了多少行,则需要使用相同的子句单独执行SELECT:

SELECT COUNT(*) AS num_rows FROM tables WHERE id = 1;

#1


Your query works according to this SQL Fiddle:

您的查询根据此SQL Fiddle工作:

http://sqlfiddle.com/#!9/84beb/1

#2


If you set a field to the value it already has, it's not counted in "affected rows". If you want to know how many rows might have been modified based on the WHERE clause, you need to do a separate SELECT with the same clause:

如果将字段设置为已有的值,则不会将其计入“受影响的行”。如果您想知道基于WHERE子句可能修改了多少行,则需要使用相同的子句单独执行SELECT:

SELECT COUNT(*) AS num_rows FROM tables WHERE id = 1;