Jump to content

MySQL Autonumber To Start at 1 again


9911782

Recommended Posts

There is no logical reason for wanting to do this, and it will break your database as auto incrementing field need to be a unique key.

 

If you are relying on these id's for sorting or something simular you are doing things wrong.

 

Explain why you think you need to do this.

Link to comment
Share on other sites

The following will give you the day number from the 1st of April (1st being 1, 2nd being 2 etc)

SELECT
     CASE
      WHEN DAYOFYEAR(NOW()) >= DAYOFYEAR(CONCAT(YEAR(NOW()),'-04-01')) THEN
        DAYOFYEAR(NOW()) - DAYOFYEAR(CONCAT(YEAR(NOW()),'-04-01')) + 1
      WHEN DAYOFYEAR(NOW()) < DAYOFYEAR(CONCAT(YEAR(NOW()),'-04-01')) THEN
        DAYOFYEAR(CONCAT(YEAR(DATE_SUB(NOW(), INTERVAL 1 YEAR)),'-12-31'))
       - DAYOFYEAR(CONCAT(YEAR(DATE_SUB(NOW(), INTERVAL 1 YEAR)),'-04-01'))
       + DAYOFYEAR(NOW())
       + 1
     END;

 

Note: the above is safe when dealing with leap years.

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.