简体   繁体   English

如何在以前的MySQL操作中获取受影响的行?

[英]How to get affected rows in previous MySQL operation?

mysql_affected_rows is to get number of affected rows in previous MySQL operation, but I want to get affected rows in previous MySQL operation. mysql_affected_rows是获取以前MySQL操作中受影响的行数,但我希望在之前的MySQL操作中获得受影响的行。 For example: 例如:

update mytable set status=2 where column3="a_variable";

Before this operation, status of some rows is already 2, and I want to get affected rows in previous MySQL operation, you can not get it by issuing a query of 在此操作之前,某些行的状态已经是2,我想在之前的MySQL操作中获取受影响的行,您无法通过发出查询来获取它

select * from mytable where status=2

So how to do this work? 那么如何做这项工作呢?

It can be efficiently and simply achieved with the following: 可以通过以下方式高效,简单地实现:

 select * from mytable where column3="a_variable" and status != 2;
 update mytable set status=2 where column3="a_variable";

The result of the first query are the rows that are going to change, and the second query actually changes them. 第一个查询的结果是要更改的行,第二个查询实际更改它们。

If this is a high performance system, you may need to take special care with transactions to prevent a new row slipping in between those 2 queries. 如果这是一个高性能系统,您可能需要特别注意事务,以防止新行在这两个查询之间滑动。

The implementation and function you are asking about is from PHP, please tag your question with PHP. 您要求的实现和功能来自PHP,请用PHP标记您的问题。

In response to your question tho, in order to do what you are suggesting you would need to keep a copy of the SQL table prior to the change and compare it with the table after the change, the resulting difference would be your answer. 为了回答你的问题,为了做你所建议的事情,你需要在更改之前保留SQL表的副本,并在更改后将其与表进行比较,结果差异就是你的答案。 You can do this in PHP by simply loading the table contents into an array and comparing the arrays before and after, any new/changed data is your answer ( Please note: it is highly not recommended you do this as it can cause an increased load on your server depending on the amount of data stored in the tables. ) 只需将表内容加载到数组中并比较前后的数组,就可以在PHP中执行此操作,任何新的/更改的数据都是您的答案( 请注意: 强烈建议不要这样做,因为它会导致负载增加在您的服务器上,具体取决于表中存储的数据量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM