Results 1 to 13 of 13

Thread: [RESOLVED] Weird

  1. #1

    Thread Starter
    Hyperactive Member ..:RUDI:..'s Avatar
    Join Date
    Aug 2005
    Location
    Yorkshire, England! c0d: Da Vinci
    Posts
    344

    Resolved [RESOLVED] Weird

    Basically I want to prune a table in the database, leaving only the top 5 posts.

    Doing this:
    PHP Code:
    mysql_query("DELETE FROM shoutbox LIMIT 5, 500"); 
    Errors this:
    #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' 500' at line 1

    Is there a certain way to prune a table or does anybody have an answer??

    Rudi
    Last edited by ..:RUDI:..; Jul 11th, 2006 at 05:58 AM.



    My Personal Home | MageBB Home | Elders Online
    Yes, Noteme is my father.

  2. #2

    Thread Starter
    Hyperactive Member ..:RUDI:..'s Avatar
    Join Date
    Aug 2005
    Location
    Yorkshire, England! c0d: Da Vinci
    Posts
    344

    Re: Weird

    That code actually wouldn't work anyway, so yeah, anyone have an idea of how to do this??

    The rows I want to keep would be the bottom 5.



    My Personal Home | MageBB Home | Elders Online
    Yes, Noteme is my father.

  3. #3

    Thread Starter
    Hyperactive Member ..:RUDI:..'s Avatar
    Join Date
    Aug 2005
    Location
    Yorkshire, England! c0d: Da Vinci
    Posts
    344

    Re: Weird

    Oooh Oooh not so resolved anymore...
    Last edited by ..:RUDI:..; Jul 11th, 2006 at 06:04 AM.



    My Personal Home | MageBB Home | Elders Online
    Yes, Noteme is my father.

  4. #4

    Thread Starter
    Hyperactive Member ..:RUDI:..'s Avatar
    Join Date
    Aug 2005
    Location
    Yorkshire, England! c0d: Da Vinci
    Posts
    344

    Re: Weird

    This doesn't work:
    PHP Code:
    mysql_query("DELETE FROM shoutbox WHERE date NOT IN (SELECT date FROM shoutbox ORDER BY date DESC LIMIT 5)"); 
    and it returns this:
    #1235 - This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'

    Only problem is... that's exactly what I want it to do!!



    My Personal Home | MageBB Home | Elders Online
    Yes, Noteme is my father.

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Weird

    Do you have any control over the server? Perhaps you can upgrade MySQL. This kind of subquery is supported starting with version 4.1, which is really stable.

    If not, you'll have to first perform the select, and then build a list of IDs in PHP code, then create the delete query from that. In that case, I'd recommend using NOT IN and fetching the IDs of the entries you want to keep - it'll keep the list shorter.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6

    Thread Starter
    Hyperactive Member ..:RUDI:..'s Avatar
    Join Date
    Aug 2005
    Location
    Yorkshire, England! c0d: Da Vinci
    Posts
    344

    Re: Weird

    Quote Originally Posted by CornedBee
    Do you have any control over the server? Perhaps you can upgrade MySQL. This kind of subquery is supported starting with version 4.1, which is really stable.
    Hmm - MySQL - 4.1.19-standard-log



    My Personal Home | MageBB Home | Elders Online
    Yes, Noteme is my father.

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Weird

    http://dev.mysql.com/doc/refman/5.0/en/subqueries.html says:
    "Starting with MySQL 4.1, all subquery forms and operations that the SQL standard requires are supported, as well as a few features that are MySQL-specific."

    http://dev.mysql.com/doc/refman/5.0/...ubqueries.html

    Hmm, perhaps the combination with LIMIT is not supported. I cannot find any mention of this in the docs, though. But I found this:
    "In general, you cannot modify a table and select from the same table in a subquery. For example, this limitation applies to statements of the following forms:

    DELETE FROM t WHERE ... (SELECT ... FROM t ...);"

    http://dev.mysql.com/doc/refman/4.1/...trictions.html

    Perhaps doing it in PHP code is indeed the simplest way.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  8. #8

    Thread Starter
    Hyperactive Member ..:RUDI:..'s Avatar
    Join Date
    Aug 2005
    Location
    Yorkshire, England! c0d: Da Vinci
    Posts
    344

    Re: Weird

    Quote Originally Posted by CornedBee
    http://dev.mysql.com/doc/refman/5.0/en/subqueries.html says:
    "Starting with MySQL 4.1, all subquery forms and operations that the SQL standard requires are supported, as well as a few features that are MySQL-specific."

    http://dev.mysql.com/doc/refman/5.0/...ubqueries.html

    Hmm, perhaps the combination with LIMIT is not supported. I cannot find any mention of this in the docs, though. But I found this:
    "In general, you cannot modify a table and select from the same table in a subquery. For example, this limitation applies to statements of the following forms:

    DELETE FROM t WHERE ... (SELECT ... FROM t ...);"

    http://dev.mysql.com/doc/refman/4.1/...trictions.html

    Perhaps doing it in PHP code is indeed the simplest way.
    Hmm, would you have any code that could do this??



    My Personal Home | MageBB Home | Elders Online
    Yes, Noteme is my father.

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Weird

    No, but the pseudo PHP code would look like this:
    Code:
    $sql = "SELECT id FROM ... WHERE you want to keep the row";
    $ids = execute_sql_and_fetch_each_id_into_an_array($sql);
    $idlist = join(', ', $ids);
    $sql = "DELETE FROM ... WHERE id NOT IN ($ids)";
    execute($sql);
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  10. #10

    Thread Starter
    Hyperactive Member ..:RUDI:..'s Avatar
    Join Date
    Aug 2005
    Location
    Yorkshire, England! c0d: Da Vinci
    Posts
    344

    Re: Weird

    PHP Code:
    static $i 1;
    $sql mysql_query("SELECT * FROM shoutbox ORDER BY date DESC LIMIT 5");
    while (
    $record mysql_fetch_assoc($sql) ) {
        
    $i++;
        
    $ids[$i] = $record['date'];

    $idlist join("', '"$ids);
    $sql1 "DELETE FROM shoutbox WHERE date NOT IN ('".$idlist."')";
    mysql_query($sql1); 
    Thanks



    My Personal Home | MageBB Home | Elders Online
    Yes, Noteme is my father.

  11. #11
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Weird

    Small modification.
    PHP Code:
    $sql mysql_query("SELECT * FROM shoutbox ORDER BY date DESC LIMIT 5");
    $ids = array();
    while (
    $record mysql_fetch_assoc($sql) ) {
        
    $ids[] = $record['date'];

    $idlist join("', '"$ids);
    $sql1 "DELETE FROM shoutbox WHERE date NOT IN ('".$idlist."')";
    mysql_query($sql1); 
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  12. #12

    Thread Starter
    Hyperactive Member ..:RUDI:..'s Avatar
    Join Date
    Aug 2005
    Location
    Yorkshire, England! c0d: Da Vinci
    Posts
    344

    Re: Weird

    Quote Originally Posted by CornedBee
    Small modification.
    PHP Code:
    $sql mysql_query("SELECT * FROM shoutbox ORDER BY date DESC LIMIT 5");
    $ids = array();
    while (
    $record mysql_fetch_assoc($sql) ) {
        
    $ids[] = $record['date'];

    $idlist join("', '"$ids);
    $sql1 "DELETE FROM shoutbox WHERE date NOT IN ('".$idlist."')";
    mysql_query($sql1); 
    Tried that but for some reason it just kept printing Array, Array, Array etc.



    My Personal Home | MageBB Home | Elders Online
    Yes, Noteme is my father.

  13. #13
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: [RESOLVED] Weird

    Huh? That shouldn't happen. What do you mean, "printing"?

    Another modification:
    Code:
    "SELECT date FROM shoutbox ..."
    No need to transfer more data from the DB to the web server than necessary.
    Last edited by CornedBee; Jul 11th, 2006 at 01:24 PM.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width