delimiter // drop procedure if exists age_limit; create procedure age_limit() begin declare i int; declare x int; set i = 0; set x = 0; while i <= 20 do set i = i + 5; SELECT COUNT(*) INTO x FROM se_ft_basic AS b INNER JOIN se_dt_sbzl_dl AS d INNER JOIN se_dt_xzqh_cmn AS c ON b.equupscode=d.equupscode AND b.dm_ds=c.DM_DS AND b.dm_qx=c.DM_QX WHERE IF("设备所在地州市"='',1,b.dm_ds=(SELECT DM_DS FROM se_dt_xzqh_cmn WHERE MC="设备所在地州市")) AND IF("设备所在区县"='',1,b.dm_qx=(SELECT DM_QX FROM se_dt_xzqh_cmn WHERE MC="设备所在区县")) #AND IF("注册登记日期开始"='',1,b.regdate>'注册登记日期开始' AND b.regdate<'注册登记日期结束') AND IF("所属设备大类名称"='',1,b.equupscode=(SELECT equupscode FROM se_dt_sbzl_dl WHERE equipname="所属设备大类名称")) AND TIMESTAMPDIFF(YEAR,b.regdate,DATE_FORMAT(NOW(), '%Y-%m-%d'))<=i GROUP BY b.equupscode; insert into age_limit (number) values (x); end while; select * from age_limit; end // UPDATE age_limit SET number=0; call age_limit(); |