【问题标题】:What am I doing wrong in this Trigger syntax?我在这个 Trigger 语法中做错了什么?
【发布时间】:2022-11-18 03:22:27
【问题描述】:

我正在尝试创建一个触发器,其中一旦将一行插入到 Vote 表中,Monitor 表中的一个字段就会递增 1——递增的行是最近的值“monitor_id”的行插入的行与 Monitor 表中的字段“staff_id”相匹配。

这是我的代码,我在 Jupyter Notebook 中运行它,并连接到我本地机器上的 MySQL 数据库:

CREATE TRIGGER update_votes_presided_over
    AFTER INSERT ON Vote
BEGIN
    UPDATE Monitor
    SET num_votes_presided = num_votes_presided + 1
    WHERE new.monitor_id == Monitor.staff_id;
END;

但我收到此错误:

* mysql+mysqlconnector://root:***@localhost/elections
   sqlite:///hw2.sqlite
(mysql.connector.errors.ProgrammingError) 1064 (42000): 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 'BEGIN
    UPDATE Monitor
    SET num_votes_presided = num_votes_presided + 1
   ' at line 3
[SQL: CREATE TRIGGER update_votes_presided_over
    AFTER INSERT ON Vote
BEGIN
    UPDATE Monitor
    SET num_votes_presided = num_votes_presided + 1
    WHERE new.monitor_id == Monitor.staff_id;
END;]
(Background on this error at: https://sqlalche.me/e/14/f405)

有人知道我做错了什么吗?

谢谢!

【问题讨论】:

    标签: mysql sql jupyter-notebook triggers


    【解决方案1】:

    我们不在 MySql 中使用 '=='

    应该是WHERE new.monitor_id = Monitor.staff_id;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多