【发布时间】:2012-10-13 22:50:11
【问题描述】:
这就是我想要做的:
当ACCOUNTS 表中有新的INSERT 时,我需要通过设置status='E' 来更新ACCOUNTS 中的行,其中pk = NEW.edit_on 表示特定(旧)帐户具有已编辑。
DELIMITER $$
DROP TRIGGER IF EXISTS `setEditStatus`$$
CREATE TRIGGER `setEditStatus` AFTER INSERT on ACCOUNTS
FOR EACH ROW BEGIN
update ACCOUNTS set status='E' where ACCOUNTS.pk = NEW.edit_on ;
END$$
DELIMITER ;
要求不是我操作 新插入的列,而是 已经存在列与pk = NEW.edit_on
但是,我无法更新同一张表:Can't update table ACCOUNTS ... already used by the statement that invoked this trigger
请提出解决方法
PS:我已经经历过Updating table in trigger after update on the same table、Insert into same table trigger mysql、Update with after insert trigger on same table 和mysql trigger with insert and update after insert on table,但他们似乎没有回答我的问题。
编辑
ACCOUNTS表:
CREATE TABLE `ACCOUNTS` (
`pk` bigint(10) unsigned NOT NULL AUTO_INCREMENT,
`user_id` bigint(9) unsigned NOT NULL,
`edit_on` bigint(10) unsigned DEFAULT NULL,
`status` varchar(1) NOT NULL DEFAULT 'A',
PRIMARY KEY (`pk`) USING BTREE) ENGINE=InnoDB AUTO_INCREMENT=2147483726 DEFAULT CHARSET=latin1
【问题讨论】:
-
如何唯一标识
ACCOUNTS中的行?如果edit_on是你的主键,那你怎么能插入重复呢? -
我已编辑问题以包含表格结构。请看。
-
如果
edit_on = 123代表pk = 456所在的行,这意味着456是123的编辑。因此,status应更新为'E'为123 -
您的架构中没有
status列。 -
哎呀..对不起,我的错。请立即查看编辑