|
-
Jul 10th, 2006, 04:36 PM
#1
Thread Starter
Hyperactive Member
[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.
-
Jul 10th, 2006, 07:07 PM
#2
Thread Starter
Hyperactive Member
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.
-
Jul 10th, 2006, 07:29 PM
#3
Thread Starter
Hyperactive Member
Re: Weird
Oooh Oooh not so resolved anymore...
Last edited by ..:RUDI:..; Jul 11th, 2006 at 06:04 AM.
-
Jul 11th, 2006, 06:06 AM
#4
Thread Starter
Hyperactive Member
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!!
-
Jul 11th, 2006, 07:17 AM
#5
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.
-
Jul 11th, 2006, 07:26 AM
#6
Thread Starter
Hyperactive Member
Re: Weird
 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
-
Jul 11th, 2006, 07:39 AM
#7
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.
-
Jul 11th, 2006, 07:49 AM
#8
Thread Starter
Hyperactive Member
Re: Weird
 Originally Posted by CornedBee
Hmm, would you have any code that could do this??
-
Jul 11th, 2006, 07:53 AM
#9
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.
-
Jul 11th, 2006, 11:24 AM
#10
Thread Starter
Hyperactive Member
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
-
Jul 11th, 2006, 01:10 PM
#11
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.
-
Jul 11th, 2006, 01:12 PM
#12
Thread Starter
Hyperactive Member
Re: Weird
 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.
-
Jul 11th, 2006, 01:19 PM
#13
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|