I want to remove a "DateJoined" field from a membership database. I would like to hold this information in the userid field. That is, I want to set MYSQL (using a trigger?) such that when a new user joins the user id assigned will be in the format: yydddxxx where yy = year, ddd = day of the year, xxx = index of user for that day.
I know how to generate the 'yyddd' portion. My question is: How can I set up the database to reset the 'userid' (AUTO_INCREMENT field) to begin counting afresh when the date changes?
Today is - 11321 (321st day of 2011)
At midnight the 'userid' field should reset to begin counting at 11322000 (322nd day of 2011) such that the first user will be assigned userid = 11322000
You can't reset the autoincrement and appear to be getting completely confused as to just what the autoincrement value represents.
Autoincrement simply provides a unique value for each entry - there is no sequence intended to be implied by the value an d there can very easily be gaps in the sequence (which don't matter because it is the uniqueness that autoincrement is intended to supply and not sequence).
There is no easy way to number entries in the order they are added as the one that someone starts adding first might end up having the next 50 finish being added before it is actually added.
If you want to be able to report on the entries in the order they were added on the day then simply save the time they were added and order them by the time when you retrieve them. You can then number them in the resultset you return from the query.
actually, yes, you can have an auto_increment that resets for each new date
however, you have to use two separate columns for this, and it has to be certain table type --
all you have to do is create a "userdate" column as CHAR(5) and give it the yyddd values, with a "usernum" subsidiary AUTO_INCREMENT column as MEDIUMINT or something like that
to show them as one column, use CONCAT in the retrieval SELECT
Thank you Rudy! Your method works as you said! It's not as easy as i thought it would be, but I can put the 2 fields together for a unique id. Now I can code the date in the userid and can drop the 'dateJoined' field. The nice thing about your solution is that when the 'userdate' changes, the 'usernum' starts at 1 again. That's what I was looking for.