Jump to content

Recommended Posts

Hi

Any idea what is SQL command to select the oldest 20 records in a Mysql table?

I know it can be done with reading the number of records and recording the oldest id and newest id and setting a range, but I am trying to find a more efficient and quicker way to do this.  (In a single SQL command).

Thanks in advance

Edited by PHP5000

Brand this would only work if I had a column saving the dates (date_created), which I do not. Also supposing I wanted to delete the records.

Any idea how to adopt this notion to DELETE, specially without date ?

Thanks in advance

 

Edited by PHP5000
7 hours ago, mac_gyver said:

you would ORDER BY the id (autoincrement primary index) column instead.

But only if you can guarantee that the oldest records are always added first. If relative age is important why would you not store the date added?

Having a date also has the advantage that you can have the option to
 

DELETE FROM tablename
WHERE datecol < CURDATE() - INTERVAL ? DAY

so you can delete all those over N days old

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.