场景:
系统运行了一段时间后,好多用户消费,需要在表中对用户进行等级更新,我们的需求是:
1、只对超过平均消费金额的用户进行等级升级
2、达到平均消费金额 1倍的用户 等级是 白金用户
3、2倍或以上的是黄金用户
4、其他一律是吃瓜用户
mysql 里面的case when:
case
when 表达式 then表达式
else 表达式
end
往往用于select 查询时 对字段进行特殊条件处理
update user_level,(select avg(user_total) as avg from user_level) b set user_rank=
case
when round(user_total/avg)>=1 and round(user_total/avg)<2 then '白金用户'
when round(user_total/avg)>=2 then '黄金用户'
ELSE
'吃瓜'
end where user_total>=b.avg
转载于:https://blog.51cto.com/chinaleo/1934988