我的数据库触发器有问题(我使用的是phpmyadmin)。当我在表'customer'中插入新行时。
+-------------+------+------------+--------------+
| 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 |
+----------+-------------+
因此,如果我插入订阅的新客户并使用“业务”组,则会在'groups_customer'中添加一行,其中id_group = 3
如果它是新订阅的“特殊”客户,则会添加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声明有关。
答案 0 :(得分:0)
GROUP
是MySQL中的保留字(used by the GROUP BY-Function)。
所以你的触发功能应该是:
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);
ELSEIF (NEW.`group`='Particular' AND NEW.subscribed=true)
THEN
INSERT INTO groups_customer (id_group, id_customer) VALUES (4, NEW.id_customer);
END IF;
END;
如果您使用的是PHPmyadmin,请记住更改此图像中的默认分隔符。 http://4.bp.blogspot.com/-u8TUx_srGrw/TVZp7saqF3I/AAAAAAAAIn8/fvYOaGcxNfY/s640/DELIMITER+MYSQL.jpg