需要将列设置为MIN(ID)
值,或者将列设置为LAST_INSERT_ID()
值是空的。所有这些步骤都应在触发器中完成。
考虑一个代码:
CREATE TRIGGER min_id_trigger BEFORE INSERT ON MyTable
FOR EACH ROW BEGIN
SET NEW.min_id = (select case when MIN(id) IS NULL then LAST_INSERT_ID() else MIN(id) FROM MyTable WHERE some_where_col = NEW.some_where_col);
END;
这给了我一个错误:
[2019-06-13 19:10:05] [42000][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 'FROM MyTable WHERE some_where_col = NEW.some_where_col);
[2019-06-13 19:10:05] END' at line 3
因此,看来select
,case when
和MySql中的触发器存在棘手的语法。
有人可以帮忙吗?