Update语句:使用case when按条件批量更新
為什么80%的碼農都做不了架構師?>>> ??
示例場景:
(1) 數據表table_001中有如下字段:column_01,?column_02,?column_03,?column_04
(2) 由于業務變更,增加了如下字段:column_10,?column_11,?column_12,?column_13
(3) 對于之前表中存在的記錄,需要根據“column_01,?column_02,?column_03,?column_04”的字段值,來填充"column_10,?column_11,?column_12,?column_13"的值。
?
示例語句:
update table_001?
?? ?set column_01 =?
?? ??? ?(case when column_10=0 then 0
?? ??? ? when column_10<120 then 1
?? ??? ? when column_10>=120 and column_10<140 then 2
?? ??? ? when column_10>=140 and column_10<160 then 3
?? ??? ? when column_10>=160 and column_10<180 then 4
?? ??? ? when column_10>=180 then 5
?? ??? ? end),
?? ??? ?column_02 =
?? ??? ?(case when column_11=0 then 0
?? ??? ? when column_11<80 then 1
?? ??? ? when column_11>=80 and column_11<90 then 2
?? ??? ? when column_11>=90 and column_11<100 then 3
?? ??? ? when column_11>=100 and column_11<110 then 4
?? ??? ? when column_11>=110 then 5
?? ??? ? end),
?? ??? ?column_03 =?
?? ??? ?(case when column_12=0 then 0
?? ??? ? when column_12<140 then 1
?? ??? ? when column_12>=140 then 2?? ?
?? ??? ? end),
?? ??? ?column_04 =?
?? ??? ?(case when column_13=0 then 0
?? ??? ? when column_13<90 then 1
?? ??? ? when column_13>=90 then 2
?? ??? ? end);
參考:http://blinkfox.com/postgresqlzhi-shi-zheng-li/
http://blog.csdn.net/sz_bdqn/article/details/8267691
?
轉載于:https://my.oschina.net/jrrx/blog/909571
總結
以上是生活随笔為你收集整理的Update语句:使用case when按条件批量更新的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: bootstrap-反色导航条
- 下一篇: jmeter跨线程组传值