欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

Update语句:使用case when按条件批量更新

发布时间:2025/7/14 编程问答 66 豆豆
生活随笔 收集整理的这篇文章主要介绍了 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按条件批量更新的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。