mysql中update按照多重条件进行更新处理的方案
作者:卖柴火的小伙子 发布时间:2024-01-22 05:14:59
标签:update,多重条件,更新
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
0
投稿
猜你喜欢
- ctrl + r => 输入drivers回车 => etc/hosts , 用记事本打开它,在 127.0.0.1 local
- 本文主要介绍了vue+elementui通用弹窗的实现(新增+编辑),分享给大家,具体如下:组件模板<el-dialog :title
- 1.简单检索数据博客内容中student表为:1.1.检索单个列select + 列名 + from + 表名1.2.检索多个列select
- Vue如何配置根目录@首先:@是在路径访问时使用的,为了减少层级引用。@这是webpack设置的路径别名,默认指向src。旧版本在build
- 设置某个字段的值自增由于某个业务需要手动操作数据库,并设置主键ID不重复。做个记录set @rownum=0;select a.id, @r
- 前言RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统。他遵循Mozilla Public License开源协议。MQ全称
- Python是动态语言,在创建对象后,可以动态地绑定属性和方法定义类:class Student: #定义类 &nb
- 下面步骤展示的是如何经过VirtualBox管理器,使得pycharm和ubuntu中的项目环境连接对应起来!如果你有属于自己的服务器,核心
- 因为最近在做文本检测相关,想试着用用百度的paddle框架。1、安装Anaconda3官网下载安装包直接运行安装,然后新建python3.7
- 是否曾经有过这样的经历:把一个元素置于另一个元素之上,而希望下面的那个元素成为可点击的?现在,利用css的pointer-events属性即
- 1、Config命令Config命令主要用于修改SSI的默认设置。其中:Errmsg:设置默认错误信息。为了能够正常的返回用户设定的错误信息
- javascript var item = document.getElementById(""); var text
- Celery简介Celery是一个简单、灵活且可靠的,处理大量消息的分布式系统,专注于实时处理的异步任务队列,同时也支持任务调度
- PHP保存session默认的是采用的文件的方式来保存的,这仅仅在文件的空间开销很小的windows上是可以采用的,但是如果我们采用uinx
- 1. 游戏是更注重于体验的产品,所以应该将游戏本省做得更加炫动和增加参与感觉。2. 网络游戏和单击游戏的区别在于社会化的添加,所以运用好这样
- MYSQLdump参数详解mysqldump备份:mysqldump -u用户名 -p密码 -h主机 数据库 a -w “sql条件” –l
- 适配器模式Adapter Pattern是什么适配器模式是一种结构型模式,它可以将一个类的接口转换成客户端所期望的接口,从而使原本不兼容的类
- Python编程中raise可以实现报出错误的功能,而报错的条件可以由程序员自己去定制。在面向对象编程中,可以先预留一个方法接口不实现,在其
- 如下所示:import urllib,json,requestsurl = 'http://127.0.0.1:8000/accou
- 选择一个合适的编辑器,比如notepad++、VS、eclipse、sublime text等,选中要集体缩进的代码块,按Tab:集体缩进(