TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Delete rows except last added 250 ones (http://www.talkphp.com/general/5274-delete-rows-except-last-added-250-ones.html)

CΛSTΞX 02-16-2010 04:08 PM

Delete rows except last added 250 ones
 
Hello mates,

I want to delete all rows in a table except last added 250 ones. I have this script, but it deletes all rows except first 250 ones. I need your help, thanks.

PHP Code:

$query="SELECT * FROM looks";

    
$result=mysql_query($query);
    echo 
mysql_error();

    
$all_looks = array();

    for(
$i=0$i<mysql_num_rows($result); $i++){
        
$row mysql_fetch_array($result);

        
$all_looks[$i] = $row["id"];
    }

    for(
$i=0$i<count($all_looks)-251$i++){
         
mysql_query("DELETE FROM looks WHERE id='".$all_looks[$i]."';");
    }

    echo 
mysql_error(); 


CΛSTΞX 02-16-2010 05:26 PM

I've solved that. Here is the solution:

PHP Code:

$ID mysql_result(mysql_query("SELECT id FROM `Table` ORDER BY id DESC limit 250,1"),0);
mysql_query("DELETE FROM `Table` WHERE id < '".$ID."'"); 



All times are GMT. The time now is 11:39 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0