表結構:spa
需求code
思路:blog
select avg(user_total) as avg from user_level
update user_level set user_rank= xxx where user_total >= 平均數
when case 表達式:class
case when 表達式 then表達式 else 表達式 end
select *, case user_total when 100 then '消費正好滿100的用戶' else '其餘' end from user_level;
select *, case when user_total > 50 and user_total < 100 then '消費超過50的用戶' when user_total > 100 then '消費超過100的用戶' else '其餘' end from user_level;
update裏邊也可使用when casedate
最終答案: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;