update case when联合left join使用

当update与when一起使用

UPDATE A
SET asset_status =(

CASE
WHEN outType =1THEN
4
WHEN outType =2THEN
5
ELSE 0
END )

//资产状态 根据 出库类型 改变 
//int型对应值在数据字典

当update与case when 与left join 一起使用

update hard_asset
left join hard_storage_out_asset on hard_asset.asset_id = hard_storage_out_asset.asset_id
left join hard_storage_out on hard_storage_out.storage_out_id = hard_storage_out_asset.storage_out_id
set   hard_asset.asset_status =
(case
    when hard_storage_out.out_type='1' then '2'
    when hard_storage_out.out_type='2' then '5'
    when hard_storage_out.out_type='3' then '7'
    when hard_storage_out.out_type='4' then '6'  
    else 0
end)

拓展:

//Case具有两种格式。简单Case函数和Case搜索函数。 
--简单Case函数 
CASE sex 
         WHEN '1' THEN '男' 
         WHEN '2' THEN '女' 
ELSE '其他' END 
--Case搜索函数 
CASE WHEN sex = '1' THEN '男' 
         WHEN sex = '2' THEN '女' 
ELSE '其他' END 

注意:Case函数只返回第一个符合条件的值,剩下的Case部分将会被自动忽略

实例使用参考博客1:https://www.cnblogs.com/clphp/p/6256207.html
2:https://www.cnblogs.com/a757956132/p/4105205.html

猜你喜欢

转载自blog.csdn.net/weixin_43889487/article/details/121851800