我需要更新我的数据库,它是基于 locationId 的多行软删除
update device d
set d.deleteDate='2016-05-07'
where d.id in (select dtg.deviceId from devicestogroups dtg
where dtg.groupId in (select g.id from `group` g
where g.locationId ='1'));
当我运行查询时出现错误:
Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column To disable safe mode, toggle the option in Preferences -> SQL Editor and reconnect.
据我所知,子查询将返回 2 条记录,因为子查询返回两条记录更新不会发生,但我该如何解决这个问题。我试图解决但没有成功。
最佳答案
试试这个,使用 join 而不是带 IN 的子查询会更好:
UPDATE device d
INNER JOIN devicestogroups dtg ON dtg.deviceId = d.id
INNER JOIN `group` g ON g.id = dtg.groupId
AND g.locationId ='1'
SET d.deleteDate='2016-05-07'
或
UPDATE device d
INNER JOIN devicestogroups dtg ON dtg.deviceId = d.id
INNER JOIN `group` g ON g.id = dtg.groupId
SET d.deleteDate='2016-05-07'
WHERE g.locationId ='1'
关于MySQL 多行更新查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40973355/