php - Deleting all but the last 30 rows in database table with MySQL -
i have simple question. how can delete last 30 rows in database table? example, take following query:
delete comments got='$user_id'
what else need in order keep last 30 rows?
this comments
table looks like:
if version if mysql supports limits within sub-queries, can use sub-query selects 30 recent rows. then, can delete except rows found in sub-query.
delete comments got='$user_id' , got not in (select got comments order timestamp desc limit 30)
if mysql version not support limits within sub-queries, you'll need split 2 separate queries:
query 1
select got comments got='$user_id' order timestamp desc limit 30
the results of query 1 should stored in single string variable (using format 1,2,3...
) , passed query 2. (the explode , implode functions may come in handy when formatting string.)
query 2
delete comments got='$user_id' , got not in ($formatted_result_from_query_1)
Comments
Post a Comment