grippat Posted January 30, 2008 Share Posted January 30, 2008 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 More sharing options...
justsomeguy Posted January 30, 2008 Share Posted January 30, 2008 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 More sharing options...
Synook Posted January 31, 2008 Share Posted January 31, 2008 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 More sharing options...
justsomeguy Posted January 31, 2008 Share Posted January 31, 2008 I see that, I guess you would use that with LIMIT to delete the top X records in a certain order. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.