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

I have a grades table, which consists of two fields: name and high score. Something like that:

----------------------- 
| name     | score    |
-----------------------
| John     | 2450     |
-----------------------
| Alice    | 2420     |
-----------------------
                         ... etc    

I need to delete all rows to the 50 best results.

Is it possible to create another temporary table?

+4
source share
3 answers

try it

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

Create an auto-increment field

alter table scores add id int unique auto_increment not null;

This will automatically write your lines in the order you select the request without conditions or order

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

Finally, delete this field.

alter table scores drop id;
+2
source

, .

: id int auto_increment null;

remove from ratings where id> 50;

change the column name of a table column,

0
source

All Articles