lot lot - 7 months ago 12
SQL Question

Is there a way to automatically update a value based on the date?

I have a table with two columns,

startdate
(datetime) and
has_started
(boolean).

The user can give in this date at any given time. Is there a way to automatically update
has_started
once
startdate
is less than today's date? A script or something? Thanks!

Answer

I would suggest that you use a view:

create view v_table as
    select t.*,
           (startdate <= curdate() then 1 else 0 end) as has_started
    from table t;

If you query the view, the has_started column will be updated when accessed. The value doesn't need to be stored in the database.

(Note: if there is a time component, you can use now() instead of curdate().)