2013-07-26 193 views
0

我有2個表today_plan和kiln_master。更新後的mysql TRIGGER更新字段

圖案之後並ITEMNO插入today_plan然後需要選擇匹配從kiln_master表一代產量和在today_plan更新該值到一代產量字段。

我創建了一個觸發

CREATE TRIGGER update_yeild AFTER INSERT ON today_plan 
FOR EACH ROW UPDATE today_plan 
    SET yeild= (SELECT kiln_master.yeild from kiln_master,today_plan WHERE today_plan.itemno = kiln_master.item AND today_plan.pattern = kiln_master.pattern) WHERE itemno=new.itemno AND pattern=new.pattern 

哪些部分是錯我的代碼

today_plan

DROP TABLE IF EXISTS decsys.today_plan; 
CREATE TABLE `today_plan` (
    `id` int(10) unsigned NOT NULL AUTO_INCREMENT, 
    `belt` varchar(25) NOT NULL, 
    `distant` varchar(25) NOT NULL, 
    `pjtno` varchar(15) DEFAULT NULL, 
    `pattern` varchar(25) NOT NULL, 
    `itemno` varchar(25) NOT NULL, 
    `pro_qty` varchar(25) NOT NULL, 
    `act_qty` varchar(25) NOT NULL, 
    `yeild` varchar(25) NOT NULL, 
    `remark` varchar(100) NOT NULL, 
    `shipment` varchar(15) NOT NULL, 
    `temp` varchar(15) NOT NULL, 
    `fire_date` date NOT NULL, 
    `kiln` varchar(10) DEFAULT NULL, 
    `kiln_plan` varchar(15) NOT NULL, 
    `kiln_act` varchar(15) NOT NULL, 
    `ins_date` date NOT NULL, 
    `ins_act` varchar(15) NOT NULL, 
    `plandate` date NOT NULL, 
    `ship_date` date NOT NULL, 
    PRIMARY KEY (`id`) 
) ENGINE=MyISAM AUTO_INCREMENT=31 DEFAULT CHARSET=utf8; 

kiln_master

DROP TABLE IF EXISTS decsys.kiln_master; 
CREATE TABLE `kiln_master` (
    `kid` int(7) NOT NULL, 
    `pattern` varchar(30) NOT NULL, 
    `item` varchar(30) NOT NULL, 
    `yeild` double NOT NULL, 
    `temp` int(6) NOT NULL, 
    `kiln` varchar(10) NOT NULL 
) ENGINE=InnoDB DEFAULT CHARSET=latin1; 

錯誤

#1442 - Can't update table 'today_plan' in stored function/trigger because it is already used by statement which invoked this stored function/trigger. 

非常感謝你

回答

1

你想它已經被插入後更新該行,是不是好做之前,所以它被插入與正確的值?那會是這樣的;

CREATE TRIGGER update_yeild BEFORE INSERT ON today_plan 
FOR EACH ROW 
    SET NEW.yeild = COALESCE((SELECT kiln_master.yeild 
        FROM kiln_master 
        WHERE NEW.itemno = kiln_master.item 
        AND NEW.pattern = kiln_master.pattern 
        LIMIT 1), 0); 
+0

親愛的Joachim Isaksson。你救了我的一天... – 9pixle