mysql中update按照多重条件进行更新处理的方案
作者:卖柴火的小伙子 时间:2024-01-22 05:14:59
1.场景问题说明
mysql中一般的update写法支持的方式是,update 表 set 字段名=修改后的字段值 where 条件1 and 条件2 and 其他条件;如果现在需求是对满足where后面的条件基础之上需对满足指定的条件数据再进行不同更新处理,那应该如何处理?如果有这样的疑问或是遇到此类场景请继续往下看.
还原一下我遇到的业务场景:现有一批会员卡,会员卡类型有次数卡和期限卡之分,期限卡余额为每天扣除一天,次数卡余额不随时间进行变化,只有使用之后才会扣除。现需要对会员卡进行更新截止时间的操作,其中只需要对期限卡更新余额操作。主要问题的难点在于会员卡类型为期限卡的会员卡,不仅需要更新有效截止时间,还需要更新会员卡的余额。为方便说明问题,简化业务如下:将会员卡id为1、2、3的截止日期更新为2022-05-01 22:50:59,其中期限卡余额更新为11.次数卡余额不做处理(会员卡id为1和2为期限卡,3为次数卡)。
会员卡表信息:
2.处理方案
2.1 使用update case when
sql如下:
UPDATE staff_card SET end_time="2022-05-01 22:50:59",update_time=NOW(),
rest_count=(CASE WHEN card_type=1 THEN 11 ELSE rest_count END) WHERE id IN (1,2,3)
配置文件写法:
<update id="updateRestCount" >
UPDATE staff_card
<set>
end_time="2022-05-01 22:50:59",update_time=NOW(),
rest_count=(CASE WHEN card_type=1 THEN 11 ELSE rest_count END)
</set>
WHERE id IN
(
<foreach collection="cardIds" item="cardId">
#{cardId}
</foreach>
)
</update>
注意写法:只对于期限卡才更新余额,次数卡余额不更新,也就是次数卡的余额rest_count字段不进行更新,直接写ELSE rest_count
,如果次数卡余额更新为其他金额,则ELSE
后面写具体的余额值.
2.2 使用if标签
组装会员卡列表信息,将每个会员卡的卡类型进行设置
ArrayList<CardInfo> cardList= new ArrayList<>();
CardInfo cardInfo = new CardInfo();
// 设置会员卡id
cardInfo.setId(1);
// 设置会员卡类型:1.期限卡;2.次数卡
cardInfo.setCardType(1);
CardInfo cardInfo2 = new CardInfo();
cardInfo2.setId(2);
cardInfo2.setCardType(1);
CardInfo cardInfo3 = new CardInfo();
cardInfo3.setId(3);
cardInfo3.setCardType(2);
cardList.add(cardInfo);
cardList.add(cardInfo2);
cardList.add(cardInfo3);
mapper接口:
public interface CardMapper {
// 更新会员卡信息
void updateRestCount(@Param("cardList") List<CardInfo> cardInfos);
}
配置文件:
<update id="updateRestCount" >
<foreach collection="cardList" item="card">
UPDATE staff_card
<set>
end_time="2022-05-01 22:50:59",update_time=NOW(),
<if test="card.cardType == 1">
rest_count =11
</if>
</set>
WHERE id=#{card.id};
</foreach>
</update>
来源:https://blog.csdn.net/weixin_43401380/article/details/124416452
标签:update,多重条件,更新
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SQL Server中如何优化磁带备份设备性能
2009-01-07 14:23:00
python创建文件时去掉非法字符的方法
2023-10-12 02:15:51
windows系统下Python环境搭建教程
2021-06-25 11:07:53
![](https://img.aspxhome.com/file/2023/4/113114_0s.png)
python和php哪个容易学
2024-05-05 09:31:52
MySql安装启动两种方法教程详解
2024-01-16 11:32:53
![](https://img.aspxhome.com/file/2023/8/128418_0s.png)
ASP+JAVAScript:复杂表单的动态生成与验证
2007-10-06 21:51:00
python实现selenium网络爬虫的方法小结
2023-01-13 03:43:38
![](https://img.aspxhome.com/file/2023/9/128199_0s.png)
sql server 2008 压缩备份数据库(20g)
2024-01-15 09:23:41
![](https://img.aspxhome.com/file/2023/5/113545_0s.png)
Javascript typeof与instanceof的区别
2024-06-07 15:53:47
asp产生不重复的随机数
2008-06-03 13:29:00
golang协程与线程区别简要介绍
2024-02-06 10:58:46
![](https://img.aspxhome.com/file/2023/1/105141_0s.png)
python使用WMI检测windows系统信息、硬盘信息、网卡信息的方法
2023-06-22 01:57:01
理解CSS3线性渐变
2010-03-28 13:42:00
![](https://img.aspxhome.com/file/UploadPic/20103/28/01-27s.jpg)
详解Numpy扩充矩阵维度(np.expand_dims, np.newaxis)和删除维度(np.squeeze)的方法
2023-02-13 22:24:24
Python机器学习k-近邻算法(K Nearest Neighbor)实例详解
2023-07-12 10:50:17
![](https://img.aspxhome.com/file/2023/7/133337_0s.png)
Python django搭建layui提交表单,表格,图标的实例
2022-09-18 19:40:19
![](https://img.aspxhome.com/file/2023/6/134856_0s.jpg)
这些关于Go中interface{}的注意事项你都了解吗
2024-02-01 08:20:19
python 用递归实现通用爬虫解析器
2022-04-15 08:13:55
![](https://img.aspxhome.com/file/2023/9/65039_0s.jpg)
在ASP.NET 2.0中操作数据之五十五:编辑和删除现有的二进制数据
2023-07-10 02:05:43
![](https://img.aspxhome.com/file/2023/3/77173_0s.gif)
asp智能脏话过滤系统v1.0
2011-04-14 11:00:00