且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-02-07 18:08:44

您可以创建触发

从MySQL 5.0.2开始包括对触发器的支持.扳机 是与表关联的命名数据库对象,并且 在表发生特定事件时激活.

Support for triggers is included beginning with 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.

以下代码在名为mytable的表上创建一个触发器,该表具有一个字段id

the following code creates a trigger on a table named mytable which has a field id

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

注意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';

这将返回已删除的ID,每个ID均以字符串开头,并以逗号开头

this will return deleted IDs each preceded by a comma in a string

的问题 查看全文