查询数据库里面最新一条记录的正确思路
- 数据库里面的记录肯定有时间字段,找到时间的最大值,在where里面查询最新的的时间
- 触发器查询的时候应该加上时间限制,不然随着时间的推移查询越来越慢
- 触发器应该是before insert类型 不然会存在递归引用
- 使用oracle函数或者mysql函数来执行时间
CREATE OR REPLACE TRIGGER "TRIG_RLRECORDMSTQY_INS_WUBODJ"
BEFORE INSERT ON RLRECORDMSTQY
FOR EACH ROW
DECLARE
delivernum WUBO_MAKECARD.DELIVERYNUM%TYPE;
BEGIN
SELECT DELIVERYNUM INTO delivernum from
WUBO_MAKECARD
WHERE VEHICLENUM = :NEW.CAR_ID AND DELIVERYSTATE <> 3
AND createtime >= TRUNC(sysdate - 35)
AND createtime = (
select max(createtime) from WUBO_MAKECARD
WHERE VEHICLENUM =:NEW.CAR_ID AND DELIVERYSTATE <> 3 AND createtime >= TRUNC(sysdate - 35)
);
IF delivernum IS NOT NULL THEN
:new.DELIVERYNUM := delivernum;
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
NULL; -- 如果没有记录,什么都不做
END;
标签:段值,触发器,记录,delivernum,MAKECARD,createtime,WUBO
From: https://www.cnblogs.com/lovefoolself/p/18439222