Jump to content

Delete rows until X number are left?


grippat
 Share

Recommended Posts

How would I write a MySQL statement that will delete all the rows in a table until there's X number left, say 200? Also, if I add ORDER BY rand() would it delete random rows until X number are left? It's sounds malicious I know, but it's just for a demo site we made that we don't want users to have access to ALL the data.

Link to comment
Share on other sites

You don't use ORDER BY with delete, you use WHERE. There's not one statement to delete rows like you want, you'll need to write a stored procedure that can use logic to determine how many rows there are and to keep deleting a row until there are X left. Check the MySQL manual for stored procedures.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...