CREATE PROCEDURE `g2`( in sts int,in type int, code varchar(20),in s int)
begin
declare i int;
declare a int;
declare b int;
declare t varchar(20) default '';
set t=DATE_FORMAT(NOW(), '%Y-%m');
set i = 1;
truncate table faw_form_a;
loop_example : loop
if (type = 1) then
select if(id,count(id),0) as id,if(status=s,s,s) as status into a,b
from faw_form_operating where status =s and
DATE_FORMAT(FROM_UNIXTIME(add_time),'%Y-%m-%d')=concat(t,'-',i) and
userid in (select userid from table_name where dealer_code=code and userid!=0);
insert into faw_form_a (id,m,c,s) values (null,i,a,b);
else
select if(id,count(id),0) as id,if(status=s,s,s) as status into a,b
from faw_form_operating where status =s and
DATE_FORMAT(FROM_UNIXTIME(add_time),'%m')=i and userid in (select userid from table_name where dealer_code=code and userid!=0);
insert into faw_form_a (id,m,c,s) values (null,i,a,b);
end if;
set i = i + 1;
if i > sts then
leave loop_example;
end if;
end loop;
select c,m,s from faw_form_a group by s,m;
end;
调用 call g2(1,2,"xxxx",1)