本文为大家介绍了关于sql语句 update字段null不能用is null问题解析,感兴趣的小伙伴记得点击关注哦。
update字段为 null 值时,
要用
set column = null,
而不是
set column is null
✖ UPDATE tableA set 字段a is null WHERE字段b = 条件;
例:
mysql> UPDATE t SET col3 is null WHERE col1 = 'a'; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'is null WHERE col1 = 'a'' at line 1
〇 UPDATE tableA set 字段a = null WHERE字段b = 条件;
例:
mysql> UPDATE t SET col3 = null WHERE col1 = 'a'; Query OK, 1 row affected (0.03 sec)
方法1:直接用sql语句方式,需要更新哪个字段就set xxclounm 即可
方法2:使用mybatis带的方法 updateById(Entry entry),传入entry对象只设置需要更新字段即可。
解析:mybatis-plus在update时对null字段有三种处理策略分别是:
默认策略是忽略null字段,所以只需要将entry中不需要更新的字段不做处理即可。注意:如果entry中字段设置为空字符串" " 则会更新进数据库
方法3:使用updateWapper,其实和方法2原理一样 如下例子只更新age字段 ,同样受更新策略影响:
UpdateWrapperupdateWrapper = new UpdateWrapper<>(); updateWrapper.eq("name","rhb"); User user = new User(); user.setAge(18); userMapper.update(user, updateWrapper);
方法1:直接用sql语句方式,需要更新哪个字段就set xxclounm 即可
方法2:使用mybatis带的方法 updateById(Entry entry),传入entry对象只设置需要更新字段即可。另外在entry类中需要更新null的字段上加上注解updateStrategy 如下面例子:
/** * 单位 */ @TableField(value = "UNIT", updateStrategy = FieldStrategy.IGNORED) private String unit;
或者设置全局更新策略(按需要设置单个字段或者全局)
# yml配置 mybatis-plus: global-config: db-config: field-strategy: not_empty
方法3:使用updateWapper,不受更新策略影响。推荐此方法,设置完策略后别人在不知情情况下可能会在不想设置null时将你设置的字段更新为空
LambdaUpdateWrapperlambdaUpdateWrapper = new LambdaUpdateWrapper<>(); lambdaUpdateWrapper.eq(User::getName, "rhb").set(User::getAge, null); Integer rows = userMapper.update(null, lambdaUpdateWrapper);
以上就是关于sql语句 update字段null不能用is null问题解析的全部内容了,感兴趣的小伙伴记得点击关注哦。