In my database, a table has a column with an integer that needs to increment every day, counting the days that have passed from a date.
Is there any way I can do this?
I know Auto Increment exists, but I don’t know if it fits for this occasion.
I found a solution using mysql events, but now I’m having trouble with the syntax.
PHPMyadmin gives me a form to complete.
I’m having trouble because I don’t know what informations to put into it.
3
Answers
You should have a look at event schedulers in MySQL, you could use them to run a job that increments your values once a day.
MySQL Event Scheduler
The best way to do this is to compute the elapsed days in a query, not to update the table every day.
For example, suppose you have a table with columns
id
andstart_date
.This query gives you the elapsed days.
Doing it this way is better than changing the table every day, for several reasons.
You can also add a generated (virtual) column to the table or use a VIEW.
The following example creates a recurring event that updates a row in a table.
First, create a new table named counter.
Second, create an event using the CREATE EVENT statement:
Replace interval timestamps, your_count and your_id with real variables