mysql錯誤碼:1064、1175、1093
1.背景
項目開發中需要將查詢出的記錄刪掉,所以直接將執行成功的select語句中的select *修改爲delete,結果執行報錯
2.錯誤碼信息
(Error Code: 1064)----(SQL語法中有錯誤。)
(Error Code: 1175)----(Mysql 中安全更新模式下不允許使用非主鍵字段更新或者刪除記錄。)
(Error Code: 1093)----(Mysql 中Delete和Update 語句,不允許子查詢中出現update和delete要操作表。)
3.排錯流程
-- 1. 查詢sql(可執行)
select * from monitorlog_1 m where cid=222222 and src_ipv4 in
(
select src_ipv4 from
(
select cid,src_ipv4,count(*) as num from monitorlog_1 where cid=222222 group by src_ipv4
) a
where num<10
)
-- 2. 修改select *爲delete的刪除sql(無法執行,錯誤碼1064)
--(將monitorlog_1 m改爲monitorlog_1即可修復成實例3)
delete from monitorlog_1 m where cid=222222 and src_ipv4 in
(
select src_ipv4 from
(
select cid,src_ipv4,count(*) as num from monitorlog_1 where cid=222222 group by src_ipv4
) a
where num<10
)
-- 3. 修改錯誤碼1064後的刪除sql(依然無法執行,錯誤碼1175)
-- (修改sql獲取主鍵id,然後根據id刪除即可修復成實例4)
delete from monitorlog_1 where cid=222222 and src_ipv4 in
(
select src_ipv4 from
(
select cid,src_ipv4,count(*) as num from monitorlog_1 where cid=222222 group by src_ipv4
) a
where num<10
)
-- 4. 修改錯誤碼1175後的刪除sql(依然無法執行,錯誤碼1093)
-- (Mysql 中Delete和Update 語句,不允許子查詢中出現update和delete要操作表。)
delete from monitorlog_1 where id in
(
select id from monitorlog_1 where cid=222222 and src_ipv4 in
(
select src_ipv4 from
(
select cid,src_ipv4,count(*) as num from monitorlog_1 where cid=222222 group by src_ipv4
) a
where num<10
)
)
-- 5. 正確sql
//查看數據庫安全模式是否打開(ON打開,OFF關閉)
show variables like 'sql_safe_updates';
//關閉安全模式
SET SQL_SAFE_UPDATES = 0;
//刪除多餘記錄
delete from monitorlog_1 where cid=222222 and src_ipv4 in
(
select src_ipv4 from
(
select cid,src_ipv4,count(*) as num from monitorlog_1 where cid=222222 group by src_ipv4
) a
where num<10
)
//打開安全模式
SET SQL_SAFE_UPDATES = 1;