简体   繁体   English

MySQL错误1064:创建过程

[英]MySQL Error 1064: Creating a procedure

I am modifying a procedure of mine. 我正在修改我的程序。 Since I switched to another webspace provider and they don't allow SQL-Trigger, I need to do the following in my procedure: 由于我切换到另一个Webspace提供程序,并且它们不允许使用SQL-Trigger,因此我需要在过程中执行以下操作:

After a comment is created for a recruitment, the recruitments "last activity"-field has to be updated. 在为招聘创建评论后,必须更新招聘“最后活动”字段。 The created comment id should also be returned. 创建的评论ID也应返回。

I tried this: 我尝试了这个:

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

But I get an error: 但是我得到一个错误:

#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

I bet it's just a small mistake again but I can't seem to find it =( 我敢打赌,这只是一个小错误,但我似乎找不到它=(

Try writing the body like this: 尝试像这样编写主体:

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;

There were several issues with your query: 您的查询存在几个问题:

  • Identify parameters with a prefix so they are not confused with columns in a query. 用前缀标识参数,这样它们就不会与查询中的列混淆。
  • Identify variables with a prefix as well. 还要标识带有前缀的变量。
  • Don't qualify the column names in the column list for a select . 不要限定select的列列表中的列名称。
  • The proper syntax for update with join in MySQL is to put the join logic right after the update . 在MySQL中使用join update的正确语法是在update之后放置join逻辑。

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

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