2 u010232070 u010232070 于 2016.03.24 14:49 提问

mysql 触发器插入数据可以,但是update没有成功,update前后的insert都执行了 5C

DROP TRIGGER IF EXISTS t_afterinsert_invitation_trigger ;
DELIMITER $$
CREATE TRIGGER t_afterinsert_invitation_trigger
AFTER INSERT ON invitation_trigger
FOR EACH ROW
BEGIN
declare _mid int;
declare _parent_level int;
declare _mids int;
declare _l int;
declare _r varchar(50);
set _mid = New.mid;
set _parent_level = New.midlevel;
set _mids = New.mids;

INSERT INTO `test`.`invitation` (`id`, `mid`, `midlevel`, `mids`, `midslevel`) 
VALUES (New.id, _mid, _parent_level, _mids, New.midslevel);

UPDATE invitation _up INNER JOIN 
(SELECT T2.mid
FROM ( 
        SELECT 
            _r AS _mids, 
    (SELECT _r = mid FROM invitation WHERE mids = _mids) AS mid, 
    _l = _l + 1 AS lvl 
        FROM 
                (SELECT _r = New.mid, _l = 0) vars, 
                invitation h 
        WHERE _r <> 0) T1 
JOIN invitation T2 
ON T1._mids = T2.mids AND T2.midlevel > New.midlevel
ORDER BY T1.lvl ASC) B
ON _up.mid = B.mid
SET _up.midlevel = New.midlevel;

INSERT INTO `test`.`invitation` (`id`, `mid`, `midlevel`, `mids`, `midslevel`) 
VALUES ('42', '14', '2', '43', '3');

END$$
DELIMITER ;

2个回答

u010232070
u010232070   2016.03.24 14:56

这个update直接在外面执行时可以修改数据的,但是放入触发器里面就没有反应了,求大神解答啊

CSDNXIAON
CSDNXIAON   2016.03.30 11:21

关于mysql 触发器(insert,update)
----------------------同志你好,我是CSDN问答机器人小N,奉组织之命为你提供参考答案,编程尚未成功,同志仍需努力!

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!