Delete all rows except first 50

Discussion in 'PHP' started by meannn, Feb 2, 2010.

  1. #1
    Hello mates, I want to delete all mysql rows in a table except last 50 rows. Please help me, thanks...

    $query="SELECT * FROM looks"; 
    
        $result=mysql_query($query);
        echo mysql_error();
    
            while($row = mysql_fetch_array($result, MYSQL_BOTH))
    
            {
                    $query1="SELECT * FROM looks where id = '".$row[1]."'"; 
    
                    $result1=mysql_query($query1);
                    $count = mysql_num_rows($result1) - 50;
    
                    mysql_query("DELETE FROM looks WHERE id='".$row[1]."' LIMIT $count",$link);
                    echo "deleted $row[1] ";
    
    }
            echo mysql_error();
    PHP:

     
    Last edited: Feb 2, 2010
    meannn, Feb 2, 2010 IP
  2. codebreaker

    codebreaker Well-Known Member

    Messages:
    281
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    110
    #2
    I'm not sure i got it right but wouldn't it be more simple to do it like :
    
    $count=mysql_query("SELECT COUNT(*) FROM looks");
    $delete=$count-50;
    mysql_query("DELETE FROM looks WHERE id<'$delete'");
    
    Code (markup):
     
    Last edited: Feb 2, 2010
    codebreaker, Feb 2, 2010 IP
  3. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #3
    Try this but I have not tested it.

    
    $query="SELECT * FROM looks ORDER BY id ASC";
    
        $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)-50; $i++){
             mysql_query("DELETE FROM looks WHERE id='".$all_looks[$i]."';");
        }
    
        echo mysql_error();
    
    Code (markup):
     
    Last edited: Feb 2, 2010
    s_ruben, Feb 2, 2010 IP
  4. yoes_san

    yoes_san Peon

    Messages:
    443
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #4
    try this

    $query="SELECT * FROM looks";
    
        $result=mysql_query($query);
        echo mysql_error();
    
            while($row = mysql_fetch_array($result, MYSQL_BOTH))
    
            {
                    [COLOR="DarkGreen"]//get the 50th id[/COLOR]
                    $query1="SELECT id FROM looks where id = '".$row[1]."' order by id asc limit 49,1"; 
    
                    $result1=mysql_query($query1);
                 
                    [COLOR="DarkGreen"]//delete anything that have id more than the 50th one[/COLOR]
                    mysql_query("DELETE FROM looks WHERE id >". $result1.",$link); 
                    echo "deleted $row[1] ";
    
    }
            echo mysql_error();
    Code (markup):
     
    yoes_san, Feb 2, 2010 IP
  5. meannn

    meannn Peon

    Messages:
    255
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Nice, but how to do it except last 50 rows ?
     
    meannn, Feb 2, 2010 IP
  6. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #6
    I have changed my post, try it again.
     
    s_ruben, Feb 2, 2010 IP
  7. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #7
    x = select count(*) from table;
    y = select id from table order by id limit x - 50, 1;
    delete from table where id < y;
     
    SmallPotatoes, Feb 2, 2010 IP
  8. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #8
    But "looks" "id"-s can be for example 1,24,58,158,159,178,254...
     
    s_ruben, Feb 2, 2010 IP
  9. meannn

    meannn Peon

    Messages:
    255
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    That worked perfectly, thanks a lot ;)
     
    meannn, Feb 2, 2010 IP
  10. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #10
    Hi,

    As I like to say, SQL is not procedural language :)
    
    DELETE FROM looks USING looks NATURAL LEFT JOIN (SELECT id FROM looks a ORDER BY id DESC LIMIT 50) A WHERE A.id IS NULL;
    
    Code (markup):
    Regards,
    Nick
    ;)
     
    koko5, Feb 2, 2010 IP