mysql用case when...then...语句实现多条件批量更新多字段脚本

之前接触过case when语句,用于计算条件列表并返回多个可能结果表达式之一~

介绍详见这篇博客:MySQL中 case函数认识和用法

本次又遇到了,但是应用场景不太一样~

记录下这次小需求:更新city表,做到指定城市为开通城市/并且设置城市的经纬度~

以下为最终脚本:

update city
     set longitude = (case when city_code = 000794 then 121.4736580000
                    when city_code = 000582 then 125.3235700000
                    when city_code = 001373 then 120.3829900000
                    when city_code = 001957 then 113.2643850000
                    when city_code = 002000 then 113.1219200000
                    when city_code = 002293 then 104.0647600000
                end),
          latitude = (case when city_code =000794 then 31.2303780000
                     when city_code = 000582 then 43.8160200000
                     when city_code = 001373 then 36.0662300000
                     when city_code = 001957 then 23.1291120000
                     when city_code = 002000 then 23.0218500000
                     when city_code = 002293 then 30.5702000000
                 end),
          opened = (case when city_code =000794 then 1
                     when city_code = 000582 then 1
                     when city_code = 001373 then 1
                     when city_code = 001957 then 1
                     when city_code = 002000 then 1
                     when city_code = 002293 then 1
                 end)
   where city_code in (000794,000582,001373,001957,002000,002293);

看起来有点傻,navicate点几下就好了嘛~

但是如果应用到多个库,写个sql脚本还是很有必要滴~

需要用到的可以参考下~ 

猜你喜欢

转载自blog.csdn.net/harry5508/article/details/89877674