0
嘿,我有一個更新後大火已定義如下觸發:我有上這樣定義插入後觸發不會引發觸發器爲每行
CREATE DEFINER = `root`@`localhost` TRIGGER `logs_m` AFTER UPDATE ON
`logs_month` FOR EACH ROW
BEGIN IF NEW.status =1 THEN INSERT INTO logs_payments(direction,
TYPE , amount, agent_id, invoice_number)
VALUES (0, 3, NEW.reward_minutes, NEW.agent_id, NULL) ,
(0, 5, NEW.credit, NEW.agent_id, NULL) ;
END IF ;
END
現在上logs_payments
:
CREATE DEFINER = `root`@`localhost` TRIGGER `payments_invoice` BEFORE INSERT ON
`logs_payments`
FOR EACH
ROW BEGIN
IF NEW.direction =0
THEN
SET NEW.invoice_number = (SELECT MAX(IFNULL(invoice_number, 100000000)) +1
FROM logs_payments l
WHERE l.direction =0) ;
END IF ;
IF NEW.direction =1 THEN SET NEW.invoice_number =
(SELECT MAX(IFNULL(invoice_number, 200000000)) +1
FROM logs_payments l
WHERE l.direction =1) ;
END IF ;
END
現在第一個插入的行invoice_number
得到空,第二個得到正確的值。
任何想法爲什麼會發生?
爲什麼地球上不使用['AUTO_INCREMENT'](http://dev.mysql.com/doc/en/example-auto-increment.html)? – eggyal
因爲該表還有其他值,所以我已經有一個auto_incremented列。並不是所有的專欄都應該有價值。 –
所以'invoice_number'不是'logs_payments'中的PK,但是它被設計爲唯一且不依賴於PK?聽起來你可以用規範化你的模式。 – eggyal