Jump to content

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.

Wow, i never knew about the PRIMARY KEY grouping option with an auto_increment value.

The real question is how is your table structured? Do you have 1 entry per day? Multiple per day?

You could possibly INSERT based on days from the 1st April...

 

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.

CREATE TABLE (
   finyear INT NOT NULL,
   id INT NOT NULL AUTO_INCREMENT,
   transaction_date DATE,
   PRIMARY KEY (finyear, id)
)

 

When apr 1st is reached set finyear to the new year value when inserting new records

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.