2016-12-05 108 views
1

我需要更新我的數據庫它是一個基於在locationId的MySQL更新查詢

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記錄,因爲我知道,因爲子查詢返回兩個記錄更新不會發生,但我該如何解決這個問題。我試圖解決但不成功。

回答

2

試試這個,它會更多更好地使用加入,而不是子查詢與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' 
+0

感謝@Suraz! – suresh

0

試試這個:

update device set deleteDate='2016-05-07' 
where id in (select dtg.deviceId from devicestogroups dtg where dtg.groupId in (select g.id from `group` g where g.locationId ='1')); 

如果執行不使用更新別名對列更新

+0

感謝您的答覆,但仍查詢無法更新爲同樣的原因 – suresh

0

試試這個:

update d 
set d.deleteDate='2016-05-07' 
from device d 
inner join devicestogroups dtg on d.id=dtg.deviceId 
inner join `group` g on g.id =dtg.groupId 
where g.locationId ='1'