Jump to content

[SOLVED] How to delete the oldest record


liam1412

Recommended Posts

I have a table that users can only have 5 records in. What query do I use to find out which is the oldest and delete if they try to insert more. Baring in mind my table had no date column and has no reason to so I want to just do it based on the primary key which is car_id.
Link to comment
Share on other sites

If your table uses an auto-increment on your key, then you simply need to do this..
[code]

$query="SELECT * FROM yourtablename WHERE user_id=$user_id ORDER BY car_id ASC"; [/code]

This would return all records sorted by car_id lowest to greatest where user_id is equal to user_id.. assuming that car_id uses the auto-increment, then the lowest number is the oldest and the highest number is the newest.

Simply count the results, then if it is greater than 4 (yes 4 because 5 is greater than 4), then delete the lowest number in the results.

Hope this helps

Post back if you need more clarification.

Link to comment
Share on other sites

[quote]In order to delete the oldest record reliably you need a date field. An autoincrement field is NOT to be relied upon for such tasks, that is NOT at all what it is designed for.[/quote]

SO supposing a did add a date field which im assuming (using now() or date() ?? ?? Which one ???) then I would obviously order by date Ascending. So then if i just use mysql_fetch_array this record taht it fetches back should be the oldest. i can then collect the car id from that and execute the delete.  Maybe I have misunderstood or im just ssuming coz now I think about it I haven't seen taht mentiond.  Man Im lost  ;D  ??? ??? ???
Link to comment
Share on other sites

Thorpe is right, an auto id field is not the best way.. I would use it depending on what the circumstances were, but for a more accurate way use a date field.

Make it a timestamp field and set it to use current timestamp you don't even have to worry about the coding for the timestamp.. it will add the current date automatically..

nate
Link to comment
Share on other sites

Sp im right in saying that when you don't use a while loop then mysql only fetch's back the oldest record  in the record set. i have only ever collected the one row need or echoed out all the rows by using a while loop.  Basically if I don't use a while loop does it bring abck the whole lot in one massive array!!!
Link to comment
Share on other sites

Im okay with adding a time column what I need to know is how to select the oldest record from the database.  Do I need to fetch them all back ro is there a way of just selecting it form the database. if I do need to fetch them all back what is the code for the picking the oldest.
Link to comment
Share on other sites

Querying my database isn't the issue.  What i don't understand is where I get the value to compare it with.  Lets just pretend im not adding a time field and using the primary key.

my code

$last5 = "SELECT * FROM last_5 WHERE userid=$userid ORDER BY car_id ASC";
$query = mysql_query($last_5);

Will return upto 5 records. if it does return 5 I want to delete the first one to make way for the new one. 

Where do I go from here. 

Link to comment
Share on other sites

For that example, put LIMIT 1 on the end of the query so it only returns the oldest.  In fact, you could use this query:

"DELETE FROM last_5 WHERE userid=$userid ORDER BY car_id ASC LIMIT 1"

I'd still add in a date column, like was earlier mentioned.  Then you can use this:

"DELETE FROM last_5 WHERE userid=$userid ORDER BY date ASC LIMIT 1"
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.