Jump to content

Delete rows until X number are left?


grippat

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

ORDER BY in a DELETE query (at least in MySQL) specifies the order in which to delete records.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...