Question

How can I make an unique variable in a table reset each day?

In other words when the day ends the unique variable starts at 0 again.

Was it helpful?

Solution

You probably can get the desired result by means of a simple trigger, assuming your version of MySQL supports those. Consider the table:

id  ephem  value1  value2
1   1412   this    that
2   2317   the     other
(&c.)

In this example, ephem is the number of seconds since midnight at the time the row was created, which obviously will reset to zero at midnight every day. You can automatically set this value at row creation time using the following trigger, which worked as intended on the MySQL 5.1.66 instance where I tested it:

CREATE TRIGGER set_ephemeral_day_value
  BEFORE INSERT ON table
  FOR EACH ROW
  SET new.ephem = CAST(MOD(UNIX_TIMESTAMP(), 86400) AS UNSIGNED INTEGER);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top