我正在修改我的程序。由于我切换到另一个网站空间提供程序并且他们不允许SQL-Trigger,我需要在我的过程中执行以下操作:
在为招聘创建评论后,必须更新招聘“最后活动”字段。还应返回创建的评论ID。
我试过了:
BEGIN
DECLARE id INT;
INSERT INTO
`comments` (
`comments`.`recruitment_id`,
`comments`.`user_id`,
`comments`.`comment`
)
VALUES (
recruitment_id,
user_id,
com
);
SELECT
LAST_INSERTED_ID()
INTO
id;
UPDATE
`recruitments`
SET
`recruitments`.`lastActivity` = `comments`.`creationDate`
INNER JOIN
`comments`
ON
`recruitments`.`id` = `comments`.`recruitment_id`
WHERE
`comments`.`id` = id;
SELECT id;
END
但是我收到了一个错误:
#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 'INNER JOIN `comments` ON `recruitments`.`id` = `comments`' at line 25
我打赌这只是一个小错误,但我似乎无法找到它=(
答案 0 :(得分:1)
尝试写下这样的身体:
BEGIN
DECLARE v_id INT;
INSERT INTO comments(recruitment_id, user_id, comment)
VALUES (v_recruitment_id, v_user_id, v_com);
SELECT v_id := LAST_INSERTED_ID();
UPDATE recruitments r INNER JOIN
comments c
ON r.id = c.recruitment_id
SET r.lastActivity = c.creationDate
WHERE c.id = v.id;
SELECT v_id;
END;
您的查询存在多个问题:
select
列列表中的列名。update
与join
的正确语法是将join
逻辑放在update
之后。