Using mysql, how do I delete all rows from a table, but keep, say, 200 records?
The obvious approach is to count them, do some arithmetic, and delete the right number. But does mysql has some builtin function that does it in one delete query?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can delete using a condition:
However your sequential could have gaps, so you would not have exactly 200 rows. So what you can do is working on your condition.
Find the records you want to keep (say the first 200) and delete everything else:
I know, that can be slow. But that’s not a production query, it’s just a clean up query. You can live with having to run it only once.