任何想法如何讓test_trigger工作?MySQL 5更新觸發器不工作後
Create table test (
book_id Int UNSIGNED NOT NULL,
book_views Int UNSIGNED NOT NULL DEFAULT 0,
Primary Key (book_id)) ENGINE = InnoDB;
Create table trigger_test (
book_id Int UNSIGNED NOT NULL,
book_views Int UNSIGNED NOT NULL DEFAULT 0,
Primary Key (book_id)) ENGINE = Memory;
delimiter $$
CREATE TRIGGER test_trigger
AFTER UPDATE ON test
FOR EACH ROW
BEGIN
DECLARE rows_count INT;
SELECT count(1) FROM trigger_test WHERE book_id=NEW.book_id INTO @rows_count;
IF @rows_count = 0 THEN
INSERT INTO trigger_test(book_id, book_views)
SELECT book_id, book_views FROM test where book_id = NEW.book_id;
ELSE
UPDATE trigger_test
SET book_views = NEW.book_views
WHERE book_id = NEW.book_id;
END IF;
END$$
delimiter ;
插入/更新不起作用。以下代碼應該在trigger_test中插入行,但不插入行。
insert into test values (1, 10);
它不插入/更新記錄。 – Maximus