这看起来很简单,但仍然是一个挑战。我尽可能简化了我的问题。
我有这个带有一条记录的 test_table:
id | cost_per_record
1 | 24
插入后,我希望表格如下所示:
id | cost_per_record
1 | 12
2 | 12
从我工作的应用程序中,我无法调用存储过程,因此我使用的代码如下:
DROP TABLE IF EXISTS `test_table`;
CREATE TABLE `test_table` (
`id` int(11) NOT NULL,
`cost_per_record` int(11) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `test_table` (`id`, `cost_per_record`) VALUES (1,24);
DELIMITER $$
CREATE TRIGGER `test_insert` BEFORE INSERT ON `test_table` FOR EACH ROW
BEGIN
update `test_table` set `cost_per_record` = 12
where `id` = 1;
END
$$
DELIMITER ;
INSERT INTO `test_table` (`id`, `cost_per_record`) VALUES
(2,12);
我通常收到的错误(也在其他尝试中):
MySQL said: Documentation
#1442 - Can't update table 'kan_test_update' in stored function/trigger because it is already used by statement which invoked this stored function/trigger
相信我,我在这个论坛上阅读了很多答案,还跑到博客上说这是不可能的。但我(仍然)不接受这一点。所以..任何解决方案...谢谢...