简体   繁体   English

在 mysql 中更新/删除并获取受影响的行 ID 列表?

[英]UPDATE/DELETE in mysql and get the list of affected row ids?

Is there an efficient way to get the list of affected row IDs (not the # of affected rows via PHP's mysql_affected_rows(), but the actual row ids that were affected) from an UPDATE or DELETE query in mysql?有没有一种有效的方法可以从 mysql 中的 UPDATE 或 DELETE 查询中获取受影响的行 ID 列表(不是通过 PHP 的 mysql_affected_rows() 获得的受影响行数,而是受影响的实际行 ID)?

In postgresql, there is a RETURNING clause within UPDATE/DELETE queries that can be used to specify values from the affected rows that are returned.在 postgresql 中,UPDATE/DELETE 查询中有一个 RETURNING 子句,可用于指定返回的受影响行的值。

In mysql, the 'brute force' way of getting the affected rows seem to be: 1. Acquire READ LOCK.在 mysql 中,获取受影响行的“蛮力”方式似乎是: 1. 获取 READ LOCK。 2. SELECT to with the WHERE condition of the UPDATE/DELETE query to get the affected row ids. 2. 使用 UPDATE/DELETE 查询的 WHERE 条件选择以获取受影响的行 ID。 3. UPDATE/DELETE. 3. 更新/删除。 4. RELEASE LOCK. 4. 解除锁定。

The above way seems very inefficient.上面的方式看起来效率很低。 Is there a more efficient way to get the affected row ids in mysql?有没有更有效的方法来获取 mysql 中受影响的行 ID?

You can create a Trigger 您可以创建触发器

Support for triggers is included beginning with MySQL 5.0.2. 从MySQL 5.0.2开始包括对触发器的支持。 A trigger is a named database object that is associated with a table, and that activates when a particular event occurs for the table. 触发器是与表关联的命名数据库对象,并在表发生特定事件时激活。

the following code creates a trigger on a table named mytable which has a field id 以下代码在名为mytable的表上创建一个触发器,该表具有字段id

CREATE TRIGGER mytable_delete
AFTER DELETE ON mytable
FOR EACH ROW SET @deletedIDs = CONCAT_WS(',', @deletedIDs, OLD.id)

notice that OLD refers to deleted row 请注意, OLD引用已删除的行

once you created a trigger on a table you can use it as follows: 一旦在表上创建了触发器,就可以按如下方式使用它:

/* empty parameter defined in CREATE TRIGGER */
Set @deletedIDs = '';
/* perform your query */
DELETE FROM mytable WHERE myotherfield = 'myfilterevalue';
/* get the parameter */
SELECT @deletedIDs AS 'Deleted_IDs';

this will return deleted IDs each preceded by a comma in a string 这将返回已删除的ID,每个ID前面都有一个字符串中的逗号

try this, it will return the updated ids as "1,2,3....": 试试这个,它会将更新后的ID返回为“1,2,3 ......”:

SET @uids := '';
UPDATE table_name
   SET some_col= 'some_val'
 WHERE some_col= 'some_val'
   AND ( SELECT @uids := CONCAT_WS(',', @uids, id) );
SELECT TRIM(LEADING ',' FROM @uids);

Using @redmoon7777 answer, it gets the updated ids and Concat them to uids , but I found out the columns are not updated.使用 @redmoon7777 答案,它获取更新的ids并将它们连接到uids ,但我发现列没有更新。

Maybe at the time of his answer, it's updating, but it's more than 8 years ago so I believe it might work on old MySQL versions.也许在他回答的时候,它正在更新,但已经超过 8 年了,所以我相信它可能适用于旧的 MySQL 版本。

But using it on version 5.7.14 and maybe higher, won't update.但是在5.7.14或更高版本上使用它不会更新。

So similar to his solution, I added extra condition to make the columns update与他的解决方案非常相似,我添加了额外的条件来更新列

SET @uids := '';
UPDATE table_name SET some_col= 'some_val' WHERE some_col= 'some_val' AND ( SELECT @uids := CONCAT_WS(',', @uids, id)) != '';
SELECT TRIM(LEADING ',' FROM @uids);

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

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