mysql sql_safe_updates 不支持子查询的更新。

时间:2023-03-09 00:24:33
mysql sql_safe_updates 不支持子查询的更新。

考虑到开发人员有时候不小心误更新数据,要求线上库的 MySQL 实例都设置 sql_safe_updates=1 来避免没有索引的 update、delete。

结果有一天开发发现下面的一个SQL 没法正确执行:

update  t1 set col2=1 where key1 in (select col2 from t2 where key2='ABcD');

错误如下:

ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column

也就是说没法对没有走到索引的where条件进行更新。搜索了下发现,的确不行。及时 key1 和key2 分别是 t1、t2 的索引[我换成主键都不行] 。说明是不支持子查询的update。

google 了一下发现人家也问过这个问题。。

http://*.com/questions/24314830/query-not-getting-executed-if-supplied-a-nested-sub-query

最后解决方法:

1)修改 session 级别的参数: set sql_safe_updates=0; 执行 update  操作。退出终端。

2)程序处理:先  select col2 from t2 where key2='ABcD' 获取数据,然后循环处理结果,并用  update t1 set col2=1 where key1=? 来批量更新过。建议还是用程序处理,临时修改变量不是长久之计。