按照條件update 字段
比如有這種場(chǎng)景
用戶(hù)消耗分?jǐn)?shù)龄恋,有兩個(gè)字段 標(biāo)記分?jǐn)?shù) 一個(gè)是每日的免費(fèi)分?jǐn)?shù) free_score
另一個(gè)是積累分?jǐn)?shù) total_score
如果 free_score > 0 , 優(yōu)先使用 free_score
如果 free_score = 0绳慎,則使用 total_score
更新語(yǔ)句如下
update card_integral t
set
t.total_score = if(t.free_score = 0 and t.total_score > 0,t.total_score - 1,t.total_score) ,
t.free_score = if(t.free_score > 0 , t.free_score - 1,t.free_score)
where t.id = 1
set 兩個(gè) 字段的順序不能顛倒,
否則 會(huì)出現(xiàn) 修改了 free_score 為0 后 吗浩, 又去 修改 total_score 的bug