【发布时间】:2016-01-22 18:40:48
【问题描述】:
我的数据库上的触发器有问题(我正在使用 phpmyadmin)。当我在表“客户”中插入新行时。
+-------------+------+------------+--------------+
| id_customer | name | group | subscribed |
+-------------+------+------------+--------------+
| 1 | John | Business | true |
| 2 | Rose | Particular | true |
| 3 | Ann | Business | false |
+-------------+------+------------+--------------+
我想在我的表“groups_customer”中添加一个新行
+----------+-------------+
| id_group | id_customer |
+----------+-------------+
| 3 | 1 |
| 4 | 2 |
+----------+-------------+
因此,如果我插入一个已订阅的新客户并使用组“Business”,它将在 id_group=3 的“groups_customer”中添加一行
如果是新订阅的“特定”客户,它将添加 id_group=4
在任何其他情况下,它都不会在“groups_customer”上添加任何行
所以这是我的触发器:
CREATE TRIGGER register_client_group
AFTER INSERT
ON customer
FOR EACH ROW
BEGIN
IF (NEW.`group`='Business' AND NEW.subscribed=true)
THEN
INSERT INTO groups_customer (id_group, id_customer) VALUES (3, NEW.id_customer);
ELSE IF (NEW.`group`='Particular' AND NEW.subscribed=true)
THEN
INSERT INTO groups_customer (id_group, id_customer) VALUES (4, NEW.id_customer);
END IF;
END;
MySQL 说:
"#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 8 "
问题似乎出在 IF 语句上。
【问题讨论】:
-
group是 mysql 中的保留字,您需要用反引号将其包装起来 ``
标签: mysql if-statement triggers phpmyadmin