如何在SQLite的触发器中比较COUNT的结果?
到目前为止,这是我想出的代码:
CREATE TRIGGER mytrigger BEFORE INSERT ON mytable BEGIN SELECT CASE WHEN SELECT COUNT (*) FROM mytable >= 3 THEN RAISE(FAIL, "Activated - mytrigger.") END; END;
它无法使用以下命令进行编译:
Error: near "SELECT": syntax error
如果我替换SELECT COUNT (*) FROM mytable >= 3为1 == 1,它将编译良好,并且触发器将始终执行。
SELECT COUNT (*) FROM mytable >= 3
1 == 1
您需要在整个SELECT语句周围添加括号 SELECT COUNT (*) FROM mytable
SELECT COUNT (*) FROM mytable
CREATE TRIGGER mytrigger BEFORE INSERT ON mytable BEGIN SELECT CASE WHEN (SELECT COUNT (*) FROM mytable) >= 3 THEN RAISE(FAIL, "Activated - mytrigger.") END; END;