我有这个表模式和数据,我不知道如何更新所有parentid 7,5和1如果在parentid 7下添加新成员示例新添加的是10.然后他所有的父母都进入了树(注意:最多10个父母只开始parentid 7在树上可以更新)7,5和1将被添加到500.
CREATE TABLE `mytree` (
`pid` INT(11) NOT NULL,
`memd` INT(11) NOT NULL,
`position` CHAR(1) NOT NULL,
`amount` DECIMAL(10,2) NOT NULL
)
COLLATE='latin1_swedish_ci'
ENGINE=MyISAM
;
pid memd position amount
0 1 1000.00
1 5 L 500.00
1 6 R 0.00
5 7 L 0.00
5 8 R 0.00
7 9 L 0.00
这是我的树。
添加新成员10后
pid memd position amount
0 1 1500.00
1 5 L 1000.00
1 6 R 0.00
5 7 L 500.00
5 8 R 0.00
7 9 L 0.00
7 10 R 0.00
修改 如果父母没有孩子但是又添加了一个孩子,父母不能收到500,或者父母的数量没有更新。
编辑最新问题
问题如果我有一个孩子的系列,然后其中一个孩子配对。,它只会更新他的直接父母的数量,他的父母的上述父母等..,没有更新,我怎么能更新那些他的父母,即使它只有一个孩子,例子9和10也应该收到金额,因为他们是 '11'的父母
pid memd position amount
0 1 1500.00
1 5 L 1000.00
1 6 R 0.00
5 7 L 500.00
5 8 R 0.00
7 9 L 0.00
9 10 L 0.00
10 11 L 0.00
11 12 L 0.00
11 13 R 0.00
我怎样才能做到这一点。
提前谢谢。
答案 0 :(得分:4)
我认为这就是你想要的
create procedure sp_update_amt(IN p_mem INT)
BEGIN
declare cnt INT;
declare par_id INT;
declare cntr INT;
declare m_mem INT;
declare s_str VARCHAR(512);
set cntr=1;
set par_id = 1;
set m_mem = p_mem;
set s_str = '';
proc_label:BEGIN
WHILE par_id != 0 DO
SELECT pid INTO par_id FROM mytree WHERE memd=m_mem;
select count(*) into cnt FROM mytree WHERE pid=par_id;
set s_str = CONCAT(s_str,cnt,cntr,par_id,m_mem,',');
set cntr = cntr+1;
set m_mem = par_id;
IF cntr <=10 THEN
update mytree set amount = amount+500
where memd=par_id;
ELSE
update mytree set amount = amount+200
where memd=par_id;
END IF;
END WHILE;
end;
SELECT s_str;
END;
尝试以上代码认为这将有效并解决您的问题 有关详细信息,请查看link
编辑和更新代码
我认为这将解决您的要求。
create procedure sp_update_amt(IN p_mem INT)
BEGIN
declare cnt INT;
declare par_id INT;
declare cntr INT;
declare m_mem INT;
declare s_str VARCHAR(512);
set cntr=1;
set par_id = 1;
set m_mem = p_mem;
set s_str = '';
proc_label:BEGIN
WHILE par_id != 0 DO
SELECT pid INTO par_id FROM mytree WHERE memd=m_mem;
select count(*) into cnt FROM mytree WHERE pid=par_id;
set s_str = CONCAT(s_str,cnt,cntr,par_id,m_mem,',');
set m_mem = par_id;
IF cnt = 2 OR cntr > 1 THEN
IF cntr <= 10 THEN
update mytree set amount = amount +500
where memd=par_id;
ELSE
update mytree set amount = amount+200
where memd=par_id;
END IF;
ELSE
LEAVE proc_label;
END IF;
set cntr = cntr+1;
END WHILE;
end;
SELECT s_str;
END;
检查您的数据和代码updated
的链接答案 1 :(得分:2)
这是你的代码
create procedure sp_update_amt(IN p_mem INT)
BEGIN
declare cnt INT;
declare par_id INT;
declare cntr INT;
declare m_mem INT;
set cntr=1;
set par_id = 1;
set m_mem = p_mem;
set s_str = '';
proc_label:BEGIN
WHILE cntr <= 10 and par_id != 0 DO
SELECT pid INTO par_id FROM mytree WHERE memd=m_mem;
select count(*) into cnt FROM mytree WHERE pid=par_id;
set cntr = cntr+1;
set m_mem = par_id;
IF cnt = 2 THEN
update mytree set amount = amount+500
where memd=par_id;
ELSE
LEAVE proc_label;
END IF;
END WHILE;
end;
END;
在inserting
row
之后sp_update_amt
拨打call sp_update_amt(inserted_memberid);
create procedure sp_update_amt(IN p_mem INT)
BEGIN
declare cnt INT;
declare par_id INT;
declare cntr INT;
declare m_mem INT;
declare s_str VARCHAR(512);
set cntr=1;
set par_id = 1;
set m_mem = p_mem;
set s_str = '';
proc_label:BEGIN
WHILE par_id != 0 DO
SELECT pid INTO par_id FROM mytree WHERE memd=m_mem;
select count(*) into cnt FROM mytree WHERE pid=par_id;
set s_str = CONCAT(s_str,cnt,cntr,par_id,m_mem,',');
set cntr = cntr+1;
set m_mem = par_id;
IF cnt = 2 THEN
IF cntr <=10 THEN
update mytree set amount = amount+500
where memd=par_id;
ELSE
update mytree set amount = amount+200
where memd=par_id;
END IF;
ELSE
LEAVE proc_label;
END IF;
END WHILE;
end;
SELECT s_str;
END;
那可行。
你可以在Link
已编辑当前版本
{{1}}
答案 2 :(得分:1)
您可以使用递归公用表表达式执行此操作,但mysql不支持它们。因此,只需编写一个存储过程即可。
以下是相关问题的链接:https://stackoverflow.com/a/8833676/4350148