cost_per_record 1

问题描述 投票:1回答:1

看似简单,但仍是一个挑战。我尽可能的简化了我的问题。

我有这个test_table,有一条记录。

    id | cost_per_record
    1  | 24

在INSERT之后,我想让这个表看起来像这样。

    id | cost_per_record
    1  | 12
    2  | 12

从我工作的应用程序中,我不能调用一个STORED PROCEDURE,所以我使用的代码和其他代码一样。

    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

相信我,我在这个论坛上读了不少答案 也在博客上读到了很多关于这个问题的文章,说这是... 不可能. 但我(仍然)不接受这个。所以......有什么解决办法吗......谢谢......。

mysql triggers database-trigger insert-update mysql-error-1442
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.