9911782 Posted February 28, 2008 Share Posted February 28, 2008 Hi I need help please... I've created a MySQL database. Capturing the data per financial year. Every year when the date is 1st April 2008, I want the autonumber to start at 1 again. How do i do that in MySQL. Thank you S Quote Link to comment https://forums.phpfreaks.com/topic/93470-mysql-autonumber-to-start-at-1-again/ Share on other sites More sharing options...
trq Posted February 28, 2008 Share Posted February 28, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/93470-mysql-autonumber-to-start-at-1-again/#findComment-478886 Share on other sites More sharing options...
luca200 Posted February 28, 2008 Share Posted February 28, 2008 http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html Quote Link to comment https://forums.phpfreaks.com/topic/93470-mysql-autonumber-to-start-at-1-again/#findComment-479048 Share on other sites More sharing options...
aschk Posted February 28, 2008 Share Posted February 28, 2008 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... Quote Link to comment https://forums.phpfreaks.com/topic/93470-mysql-autonumber-to-start-at-1-again/#findComment-479114 Share on other sites More sharing options...
aschk Posted February 28, 2008 Share Posted February 28, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/93470-mysql-autonumber-to-start-at-1-again/#findComment-479155 Share on other sites More sharing options...
Barand Posted February 29, 2008 Share Posted February 29, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/93470-mysql-autonumber-to-start-at-1-again/#findComment-480530 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.