0

I have a table with 2 columns. Days is the days from Start Date to today.

How should Trigger be to automatically increase Days every day?

Start Date      Days  
19/07/17         19          
25/07/17         13
07/08/17         0

2 Answers 2

3

Triggers only work on the rows which are affected by the statement causing the trigger to execute.

When you want to update a whole table, use a normal statement, not triggers.

When you have data, that can be easily calculated, don't store this in a table. Either you calculate it in your SELECT statement or create a view.

Example:

CREATE VIEW view_with_day_difference_calculated AS
SELECT start_date, TIMESTAMPDIFF(day, start_date, NOW()) AS days
FROM your_table;

Then all you have to do is this in the future.

SELECT * FROM view_with_day_difference_calculated;
0

Another option would be to add the days calculation as a computed column.

3
  • Computed columns are not supported by SQLite AFAIK.
    – andrews
    Oct 18, 2017 at 8:57
  • Could you please describe this in more detail? Oct 18, 2017 at 9:58
  • My apologies i thought this was for sql server. Oct 19, 2017 at 6:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.