我无法弄清楚这个插入触发器做错了什么。正在尝试读取目标表的最后一行,并相应地设置状态。错误信息将我指向手册,这是我开始的地方;(。。组件查询可以工作,但组合起来不多。。
MariaDB [tide]> desc Marsh;
+--------+--------------+------+-----+---------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+--------------+------+-----+---------------------+-------+
| time | timestamp | NO | PRI | current_timestamp() | |
| Sensor | int(3) | YES | | NULL | |
| NOAA | decimal(3,2) | YES | | NULL | |
| rNum | int(1) | YES | | NULL | |
| state | varchar(5) | NO | | slack | |
+--------+--------------+------+-----+---------------------+-------+
我的触发
DELIMITER $$
CREATE TRIGGER trend_before_insert
BEFORE INSERT
ON Marsh FOR EACH ROW
BEGIN
DECLARE LASTREAD decimal(3,2) DEFAULT 0
SELECT NOAA
INTO LASTREAD
FROM Marsh
WHERE time in (select max(time) from Marsh);
IF NOAA > LASTREAD THEN
SET state = 'flow';
END IF;
IF NOAA < LASTREAD THEN
SET state = "ebb";
END IF;
END$$
DELIMITER ;
数据样本:
| 2021-05-04 12:25:28 | 175 | 1.26 | 6 | slack |
| 2021-05-04 12:35:31 | 175 | 1.26 | 1 | slack |
| 2021-05-04 12:45:28 | 175 | 1.26 | 2 | slack |
密钥查询:MariaDB[tide]>从Marsh选择NOAA,其中时间在(从Marsh中选择最大(时间((;
+------+
| NOAA |
+------+
| 1.26 |
+------+
1 row in set (0.001 sec)
返回的错误是error 1064(42000(-不幸的是,我非常有意地在已发布的示例上建模语法。这只是一个业余项目,这里唯一的利害关系是我持续的无知。对于更好的方式的建议——无论是什么——都将受到赞赏。
您漏掉了一个分号DEFAULT 0 之后
你也需要新的。用于寻址插入行的列的前缀
DELIMITER $$
CREATE TRIGGER trend_before_insert
BEFORE INSERT
ON Marsh FOR EACH ROW
BEGIN
DECLARE LASTREAD decimal(3,2) DEFAULT 0;
SELECT NOAA
INTO LASTREAD
FROM Marsh
WHERE time in (select max(time) from Marsh);
IF NEW.NOAA > LASTREAD THEN
SET NEW.state = 'flow';
END IF;
IF NEW.NOAA < LASTREAD THEN
SET NEW.state = "ebb";
END IF;
END$$
DELIMITER ;