3条回答 默认 最新
- Imadone 2017-09-04 11:20关注
如果你能用mysq5.7,则可以使用虚拟列,不需要循环和游标。
先给你的表创建一个虚拟列(其实就是索引之类的东西):
alter table订单
add columna
tinyint(1) generated always as(1) virtual;
然后执行sql得出结果:
select concat(left(count(1)/(select sum(a) from订单
where流水单号
in (select流水单号
from订单
where种类
='冷藏奶'))*100,2),'%') as '频率',count(1) as '出现次数',种类
from订单
where sex in (select sex from订单
where种类
='冷藏奶') group by种类
order by count(1) desc;in的效率可能比较低,自行优化下。
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报