我正在编写一个存储过程,它将按酒精类别和工作编号显示是/否计数。我开始通过联合对当前的类别(葡萄酒、啤酒、威士忌)进行编程,但提前考虑可能会有更多类别,并且我想象这个代码的大小会越来越大。是否可以在循环中执行联合,然后传递酒精类别参数?在互联网上大肆浏览,在这个主题上看到的很少,所以任何帮助或指导将不胜感激。
我的代码的开始......
delimiter $$
create procedure alc_cat_yn (in jid int)
begin
select
cast(concat(jobid,' - Wine')
as char(50)) as `Job Number - Consumed Yesterday`
,sum(case when wine_id=1 then 1 else 0 end) as y
,sum(case when wine_id=2 then 1 else 0 end) as n
from demos
where jobid=jid
group by jobid
union all
select
cast(concat(jobid,' - Beer')
as char(50)) as `Job Number - Consumed Yesterday`
,sum(case when beer_id=1 then 1 else 0 end) as y
,sum(case when beer_id=2 then 1 else 0 end) as n
from demos
where jobid=jid
group by jobid
union all
select
cast(concat(jobid,' - Whisky')
as char(50)) as `Job Number - Consumed Yesterday`
,sum(case when whisky_id=1 then 1 else 0 end) as y
,sum(case when whisky_id=2 then 1 else 0 end) as n
from demos
where jobid=jid
group by jobid;
end