DROP TABLE IF EXISTS `test`;CREATE TABLE IF NOT EXISTS `test` 
(
  `id` int(11) ,
  `parent` int(11) 
  `name` varchar(20) DEFAULT NULL,
  `cost` float DEFAULT NULL,
  `budget` float DEFAULT NULL,
  `progress` float DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;
ALTER TABLE `test` ADD CONSTRAINT PK_TEST1 PRIMARY KEY (`id`);
ALTER TABLE `test` ADD CONSTRAINT FK_TEST1_PARENT_TEST FOREIGN KEY(`parent`) 
REFERENCES test(`id`);INSERT INTO `test`(`id`,`parent`,`buget`) VALUES (1,-1,200);
INSERT INTO `test`(`id`,`parent`,`buget`) VALUES (2,1,100);
INSERT INTO `test`(`id`,`parent`,`buget`) VALUES (3,1,100);
delimiter //DROP FUNCTION IF EXISTS test_sum//
CREATE FUNCTION test_sum(p_parent int) RETURNS FLOAT
BEGIN
DECLARE result FLOAT DEFAULT 0.0;
SELECT SUM(cost) into result FROM test where id in(select id from test where parent = p_parent);
        IF(result IS NULL) then 
 set result = 0;
        END IF ;
RETURN result;
end//drop function if exists getParentList;//
create function getParentList(childId int) returns varchar(1000)
begin
declare sTemp varchar(1000);
declare sTempPar int;
declare l_cost float default 0;
declare l_budget float default 0;
declare l_parent int;
set sTemp = '$';
set sTempPar = childId;
select cost into l_cost from test where id=childId;
select budget into l_budget from test where id=childId;
UPDATE test SET progress = l_cost/l_budget where id = childId;
while sTempPar != -1 do
select budget into l_budget from test where id = sTempPar;
select parent into l_parent from test where id = sTempPar;
set sTemp = concat(sTemp,',',l_parent);
update test set cost=test_sum(l_parent) where id=l_parent;
IF(sTempPar != childId) then
UPDATE test SET progress = test_sum(sTempPar)/l_budget where id=sTempPar;
end if;
select group_concat(parent) into sTempPar from test where parent<>id and find_in_set(id,sTempPar)>0;
end while;
return sTemp;
end//deimiter ;现在想实现的功能是当我插入一条语句:INSERT INTO `test`(`id`,`parent`,`cost`,`buget`) VALUES (4,2,60,70);
更新id in(1,2)的cost和progress;
progress=cost/budget
本来这个功能已经在getParentList(childId)的函数中实现了,但是我想用触发器来实现,各位大神能不能帮帮忙啊?也就是说,现在我再插入一条语句:insert into test(id,parent,cost,budget) values(5,4,40,50);
现在要更新的是id in(1,2,4)的cost和progress