Leave only first 50 records in SQL database and delete the rest

岁酱吖の 提交于 2019-12-01 07:31:12

please try this

delete from scores_tbl Where
id not in
(select * from
(select id from scores_tbl order by score desc limit 50)
 as temp)

Create an auto increment field

alter table scores add id int unique auto_increment not null;

This will automatically number your rows in the order of a select query without conditions or order-by

select * scores;
delete from scores where id > 50;

Finally, remove that field

alter table scores drop id;

You need a unique field for this , so either alter the table before and after the delete.

Like : alter table scores add id int unique auto_increment not null;

delete from scores where id > 50;

alter table drop column id;

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