BEGIN
CREATE TEMPORARY TABLE IF NOT EXISTS t4(
flag VARCHAR(1),
licensetype VARCHAR(2),
carno VARCHAR(16),
carno_original VARCHAR(16),
pointcodeEnt VARCHAR(12),
pointcodeExt VARCHAR(12),
pointnameEnt VARCHAR(100),
pointnameExt VARCHAR(100),
devicecode VARCHAR(18),
devicecodeExt VARCHAR(18),
cpicEnterPath VARCHAR(200),
cpicExitPath VARCHAR(200),
carcolor VARCHAR(4),
plate_position VARCHAR(45),
collectiondate1 DATETIME,
collectiondate2 DATETIME
);
SET @flag:= '1';
INSERT INTO t4 SELECT
@flag:= '2' AS flag,
@licensetype:=IFNULL(t2.licensetype,t3.licensetype) AS licensetype,
@carno:= IFNULL(t2.carno,t3.carno) AS carno,
@carno_original:= IFNULL(t2.carno_original,t3.carno_original) AS carno_original,
@pointcodeEnt:=IFNULL(t2.pointcode,'') AS pointcodeEnt,
@pointcodeExt:=IFNULL(t3.pointcode,'') AS pointcodeExt,
@pointnameEnt:=IFNULL(t2.pointname,'') AS pointnameEnt,
@pointnameExt:=IFNULL(t3.pointname,'') AS pointnameExt,
@devicecode:=IFNULL(t2.devicecode,'') AS devicecode,
@devicecodeExt:=IFNULL(t3.devicecode,'') AS devicecodeExt,
@cpicEnterPath:=t2.cpic1path AS cpicEnterPath,
@cpicExitPath:=t3.cpic1path AS cpicExitPath,
@carcolor:=IFNULL(t2.carcolor,t3.carcolor) AS carcolor,
@plate_position:=IFNULL(t2.plate_position,t3.plate_position) AS plate_position,
@collectiondate1:=IFNULL(t2.collectiondate,NULL) AS collectiondate1, -- 入口采集时间
@collectiondate2:=IFNULL(t3.collectiondate,NOW()) AS collectiondate2
FROM park_detect_info t2
INNER JOIN park_detect_info t3 ON NEW.exit_nid = t3.nid
WHERE t2.nid = NEW.enter_nid;
IF @flag = '1' THEN
INSERT INTO t4 SELECT
@flag:= '2' AS falg,
@licensetype:= t2.licensetype AS licensetype,
@carno:= t2.carno AS carno,
@carno_original:= t2.carno_original AS carno_original,
@pointcodeEnt:= '' AS pointcodeEnt,
@pointcodeExt:= IFNULL(t2.pointcode,'') AS pointcodeExt,
@pointnameEnt:= '' AS pointnameEnt,
@pointnameExt:= IFNULL(t2.pointname,'') AS pointnameExt,
@devicecode:= '' AS devicecode,
@devicecodeExt:= IFNULL(t2.devicecode,'') AS devicecodeExt,
@cpicEnterPath:= '' AS cpicEnterPath,
@cpicExitPath:= t2.cpic1path AS cpicExitPath,
@carcolor:= t2.carcolor AS carcolor,
@plate_position:=t2.plate_position AS plate_position,
@collectiondate1:= NULL AS collectiondate1, -- 入口采集时间
@collectiondate2:= t2.collectiondate AS collectiondate2
FROM park_detect_info t2 WHERE t2.nid = NEW.exit_nid; -- 如果出口存在,则用出口的
END IF;
IF @flag = '1' THEN
INSERT INTO t4 SELECT
@flag:= '2' AS falg,
@licensetype:=t2.licensetype AS licensetype,
@carno:= t2.carno AS carno,
@carno_original:= t2.carno_original AS carno_original,
@pointcodeExt:='' AS pointcodeExt,
@pointcodeEnt:=IFNULL(t2.pointcode,'') AS pointcodeEnt,
@pointnameEnt:=IFNULL(t2.pointname,'') AS pointnameEnt,
@pointnameExt:='' AS pointnameExt,
@devicecode:=IFNULL(t2.devicecode,'') AS devicecode,
@devicecodeExt:='' AS devicecodeExt,
@cpicEnterPath:=t2.cpic1path AS cpicEnterPath,
@cpicExitPath:='' AS cpicExitPath,
@carcolor:=t2.carcolor AS carcolor,
@plate_position:=t2.plate_position AS plate_position,
@collectiondate1:=t2.collectiondate AS collectiondate1,
@collectiondate2:=NULL AS collectiondate2
FROM park_detect_info t2 WHERE t2.nid = NEW.enter_nid; -- 如果出口不存在,则用入口的
END IF;
IF @flag = '2' THEN
SET NEW.`licensetype` = @licensetype; SET @licensetype:= NULL;
SET NEW.carno = @carno; SET @carno:= NULL;
SET NEW.carno_original = @carno_original;SET @carno_original:= NULL;
SET NEW.pointcodeEnt = @pointcodeEnt; SET @pointcodeEnt:= NULL; SET NEW.pointcodeExt = @pointcodeExt;SET @pointcodeExt:= NULL;
SET NEW.pointnameEnt = @pointnameEnt; SET @pointnameEnt:= NULL; SET NEW.pointnameExt = @pointnameExt;SET @pointnameExt:= NULL;
SET NEW.devicecode = @devicecode;SET @devicecode:= NULL;
SET NEW.devicecodeExt = @devicecodeExt;SET @devicecodeExt:= NULL;
SET NEW.cpicEnterPath = @cpicEnterPath; SET @cpicEnterPath:= NULL;
SET NEW.cpicExitPath = @cpicExitPath;SET @cpicExitPath:= NULL;
SET NEW.carcolor = @carcolor;SET @carcolor:= NULL;
SET NEW.plate_position = @plate_position;SET @plate_position:= NULL;
SET NEW.collectiondate1 = @collectiondate1;SET @collectiondate1:= NULL;
SET NEW.collectiondate2 = @collectiondate2;SET @collectiondate2:= NULL;
SET @flag:= '1';
END IF;
DELETE FROM t4 WHERE 1=1;
END
以前写过的一个坑爹的mysql 触发器,看看是否有参考价值。