我试图在更新后构建一个简单的触发器来更新另一个表,但我在这里找不到语法错误。要做到这一点,我将使用phpMyAdmin向导。
错误:
MySQL:#1064 - 您的SQL语法出错;检查手册 对应于您的MySQL服务器版本,以获得正确的语法 在'UPDATE question SET question.fname_author = new.fname附近使用, 第4行的问题.lname_au'
TABLES STRUCTURE:
question user_question users
- id PK -id_user PFK -id PK
- fname_author -id_question PFK -fname
- lname_author -fname_author FK -lname
- question -lname_author FK -email
- category -question FK -office
- level -office FK -password
- created_at -created_at -created_at
- updated_at -updated_at -updated_at
CREATE TRIGGER `TAU_users_UPDATE_dati_domanda` AFTER UPDATE ON `users`
FOR EACH ROW
BEGIN
SET @id_question = (SELECT user_question.id_question
FROM user_question
WHERE user_question.id_user =new.id);
UPDATE question
SET
fname_author = new.fname,
lname_author = new.lname
WHERE question.id = @id_question
END;
答案 0 :(得分:0)
尝试改变:
CREATE TRIGGER `TAU_users_UPDATE_dati_domanda` AFTER UPDATE ON `users`
FOR EACH ROW
BEGIN
SET @id_question = (SELECT user_question.id_question
FROM user_question
WHERE user_question.id_user =new.id)
UPDATE question
SET
question.fname_author = new.fname,
question.lname_author = new.lname
WHERE question.id = @id_question
END;
在:
delimiter //
CREATE TRIGGER `TAU_users_UPDATE_dati_domanda` AFTER UPDATE ON `users`
FOR EACH ROW
BEGIN
SET @id_question = (SELECT user_question.id_question
FROM user_question
WHERE user_question.id_user =new.id);
UPDATE question
SET
fname_author = new.fname,
lname_author = new.lname
WHERE question.id = @id_question;
END;//
delimiter ;
有关触发器的语法和示例,请参阅:http://dev.mysql.com/doc/refman/5.6/en/trigger-syntax.html以及有关使用BEGIN ... END
内部触发器的相关注释。
答案 1 :(得分:0)
@Renzo提供的答案应该有效......
我正在尝试使用一些测试数据测试o / p代码......通常为时已晚......
无论如何,无论如何我都会发布它......
所以我创建了一个带有一些测试数据的SQLFddle。
触发码:
CREATE TRIGGER `TAU_users_UPDATE_dati_domanda` AFTER UPDATE ON `users`
FOR EACH ROW
BEGIN
SET @id_question = (SELECT user_question.id_question
FROM user_question
WHERE user_question.id_user =new.id);
UPDATE question
SET
question.fname_author = new.fname,
question.lname_author = new.lname
WHERE question.id = @id_question;
END;//
不重要 - 请注意:update
中的合格列名称。
之前的测试数据:
用户:
id fname lname email office password Created_at
1 fname_u1 lname_u1 u1@here.com office_u1 pwd_u1 September, 17 2015 11:57:11
2 fname_u2 lname_u2 u2@here.com office_u2 pwd_u2 September, 17 2015 11:57:12
问题:
id fname_author lname_author question category level created_at
1 fname_q1 lname_q1 what question q1? cat_1 1 September, 17 2015 11:57:12
2 fname_u2 lname_u2 what question q2? cat_2 2 September, 17 2015 11:57:12
更新声明:
UPDATE users
SET users.fname = 'fname_u2_new_fn01',
users.lname = 'lname_u2_new_fn01'
WHERE users.id = 2//
更新后的测试数据:
用户:
id fname lname email office password Created_at
1 fname_u1 lname_u1 u1@here.com office_u1 pwd_u1 September, 17 2015 11:57:11
2 fname_u2_new_fn01 lname_u2_new_fn01 u2@here.com office_u2 pwd_u2 September, 17 2015 11:57:12
问题:
id fname_author lname_author question category level created_at
1 fname_q1 lname_q1 what question q1? cat_1 1 September, 17 2015 11:57:12
2 fname_u2_new_fn01 lname_u2_new_fn01 what question q2? cat_2 2 September, 17 2015 11:57:12