更新數據技巧之:update表子查詢、多條件判斷

場景:

系統運行了一段時間後,好多用戶消費,需要在表中對用戶進行等級更新,我們的需求是:


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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章