56,677
社区成员
发帖
与我相关
我的任务
分享
//例如: id num
1 0
2 0
3 0
例如我给定的固定值是6:求mysql语句更新上面3条记录(也可能是n条)num的和为6,无论3条num的值是(2,2,2)或(1,2,3)都行 只要它们的和是6就行~!
CREATE
PROCEDURE ps_update_num( IN ps_num int(11) CHARACTER SET utf8)
BEGIN
DECLARE done1,INT DEFAULT 0;
DECLARE ps_id,ps_totals INT DEFAULT 0;
DECLARE ps_totals_id VARCHAR(100) DEFAULT '0';
DECLARE cur1 cursor for select id table_name ;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done1 = 1;
open cur1;
repeat
FETCH cur1 INTO ps_id;
if not done1 then
if( ps_id = ps_num ) then
update table_name set num=ps_num;
end if ;
if( ps_id < ps_num) then
set ps_totals += ps_id ;
set ps_totals_id =concat(ps_totals_id,",",ps_id);
if( ps_totals =ps_num) then
update table_name set num=ps_num;
end if ;
if( ps_totals >ps_num) then
set ps_totals =0 ;
set ps_totals_id='0';
end if ;
end if ;
end IF;
until done1 end
REPEAT;
CLOSE cur1;
commit;
END