如何在SELECT语句中修复MySQL中的语法错误

时间:2019-11-10 22:01:09

标签: mysql sql

从第8行的SELECT语句开始获取语法错误。不知道为什么。有任何想法吗?在错误开始的地方查看代码中的注释。

我认为这可能与第二次登录Login有关,但是当我尝试将其更改为Login o时,仍然遇到相同的错误。

    CREATE PROCEDURE setAttendance(IN _sectionid integer, IN _studentid integer, IN _logindate datetime, IN _result integer)

BEGIN

UPDATE Login l
INNER JOIN Roll r ON l.rollid = r.tid
SET result = _result
WHERE
r.sectionid = _sectionid AND r.studentid = _studentid 
AND DATE(logindate) = DATE(_logindate);

//error starts here and ends on line after it
SELECT tid FROM Login l
INNER JOIN Roll r ON l.rollid = r.tid
WHERE
r.sectionid = _sectionid AND r.studentid = _studentid 
AND DATE(logindate) = DATE(_logindate);

我只想运行此命令而不会出现语法错误

1 个答案:

答案 0 :(得分:0)

此问题通常是由于缺少DELIMITER而引起的,但是我还建议简化日期算术,以便它对索引更友好并且限定 all 列名:

DELIMITER $$

CREATE PROCEDURE setAttendance (
    IN _sectionid integer,
    IN _studentid integer,
    IN _logindate datetime,
    IN _result integer
)
BEGIN

    UPDATE Login l INNER JOIN
           Roll r
           ON l.rollid = r.tid
        SET result = _result
    WHERE r.sectionid = _sectionid AND 
          r.studentid = _studentid AND
          ?.logindate >= DATE(_logindate) AND   -- the ? is for the table alias
          ?.logindate < DATE(_logindate) + INTERVAL 1 DAY;


    //error starts here and ends on line after it

    SELECT r.tid    -- table alias
    FROM Login l INNER JOIN 
         Roll r
         ON l.rollid = r.tid
    WHERE r.sectionid = _sectionid AND
          r.studentid = _studentid AND
          ?.logindate >= DATE(_logindate) AND   -- the ? is for the table alias
          ?.logindate < DATE(_logindate) + INTERVAL 1 DAY;
END; $$

DELIMITER ;

请注意,问号是为了让您包括适当的表别名。限定所有列名称。