如何删除--safe-updates模式

时间:2016-06-18 02:21:25

标签: mysql sql-delete

e.g。我想删除下表中的最新三条记录

root@localhost:[test]10:14:02>select * from delete_test order by create_time desc;
+----+------+---------------------+
| id | code | create_time         |
+----+------+---------------------+
|  9 | fff  | 2016-06-18 10:08:28 |
|  8 | eee  | 2016-06-18 10:08:21 |
|  7 | ddd  | 2016-06-18 10:08:15 |
|  3 | ccc  | 2016-06-17 22:18:48 |
|  2 | bbb  | 2016-06-17 22:18:42 |
|  1 | aaa  | 2016-06-17 22:18:37 |
+----+------+---------------------+

首先我尝试了

root@localhost:[test]10:14:14>delete from delete_test where code in (select code from delete_test order by create_time desc limit 3);
ERROR 1235 (42000): This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'

然后我试了

root@localhost:[test]10:17:05>delete a from delete_test a, (select code from delete_test order by create_time desc limit 3) b where a.code = b.code;
ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column

但提交的codeunique key

然后我试了

root@localhost:[test]10:18:49>delete a from delete_test a join (select code from delete_test order by create_time desc limit 3) b on a.code = b.code where a.id>0;
ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column

但我明确地使用了primary key

所以我想知道如何在--safe-updates模式中删除最新的一些记录?

1 个答案:

答案 0 :(得分:0)

你可以这么简单:

delete from delete_test order by create_time limit 3;

在此处查看:http://sqlfiddle.com/#!9/04f68/1

如果我没有按顺序排列,只需将order by部分更改为order by create_time desc