繁体   English   中英

MySQL错误1064:创建过程

[英]MySQL Error 1064: Creating a procedure

我正在修改我的程序。 由于我切换到另一个Webspace提供程序,并且它们不允许使用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

我敢打赌,这只是一个小错误,但我似乎找不到它=(

尝试像这样编写主体:

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的列列表中的列名称。
  • 在MySQL中使用join update的正确语法是在update之后放置join逻辑。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM