在sql数据库中统计价格小于10,小于60大于10,小于600大于60的商品数量

老师提示用sum if 先在这里谢过啦
2025-04-29 21:23:20
推荐回答(2个)
回答1:

select sum(case when price <= 10 then 1 else 0 end) 小于10,
sum(case when price > 10 and price <= 60 then 1 else 0 end) 小于60大于10,
sum(case when price > 60 and price <= 600 then 1 else 0 end) 小于600大于60,
sum(case when price > 600 then 1 else 0 end) 其他
from 表

回答2:

select sum(if(价格<10,1,0)),sum(if(价格>10 and 价格<60,1,0)),sum(if(价格>60 and 价格<600,1,0)) from 表