Jump to content

[SOLVED] mysql limit and delete


andyd34

Recommended Posts

I need to show the last 20 members that have visited a page. I know how to get the results but how would I limit the stored names to 20 in a mysql-db.

 

I don't mean

mysql_query("SELECT * FROM SOMEWHERE LIMIT 0,20");

 

I mean delete entries greater than 20 but keep the 20 newest

Link to comment
Share on other sites

Hello,

 

Make sure your table has a timestamp, and then sort on the date, most recent first fot the last 20

 

mysql_query("SELECT * FROM SOMEWHERE ORDER BY yourtime_stamp DESC LIMIT 0,20");

 

I hope this helps

 

Richard

Link to comment
Share on other sites

Hi, Thanks for that but the issues isn't fetching the information its deleting it. I don't want to fill the database up with unwanted information.

 

for instance the db holds records 1 to 20. some visits the page and it holds records 1 to 21, i want to delete record 1. then someone else visits so its now 2 to 22, i want to delete record 2.

 

but thanx anyway

Link to comment
Share on other sites

MySQL is built for storing huge amounts of data very efficiently. thousands of rows arent a problem, it handles millions well also.

 

But if you do want to do that there's a couple of options, what's your table layout?

Link to comment
Share on other sites

  • 2 weeks later...

HI and thanks for all your replies. I have now sorted the problem and just thught i'd post the solution I came up with

 


$sql = mysql-query("SELECT * FRON table WHERE column = 'something' AND Usr_ID = 'myid' LIMIT 0,20 ORDER BY date DESC");
while($row=mysql_fetch_assoc($sql)) {
$found[] = $row['id'];
### RESULTS HERE ###
}
mysql_query("DELETE FROM table WHERE id NOT IN (".implode(', ', $found).") AND Usr_ID = 'myid'");

 

Well it worked for me just thought it may come in usefull to someone else

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.