how to delete the latest some records in --safe-updates mode

孤街醉人 提交于 2019-12-12 03:44:12

问题


e.g. I want to delete the latest three records in below table

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 |
+----+------+---------------------+

first I tried

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'

then I tried

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

but code filed is unique key.

then I tried

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

but I used primary key explicitly.

So I want to know how to delete the latest some records in --safe-updates mode?


回答1:


You can do as simple as:

delete from delete_test order by create_time limit 3;

See it working here: http://sqlfiddle.com/#!9/04f68/1

If I didn't put in the right order just change the order by part to order by create_time desc



来源:https://stackoverflow.com/questions/37892657/how-to-delete-the-latest-some-records-in-safe-updates-mode

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!