DATE_SUB with configurable value
Question
In MySQL you can use the DATE_SUB function to get a new date based on the values passed in. For example I can use the following to get the a date of 30 days ago from when I run the function
SELECT DATE_SUB(NOW(), INTERVAL 30 DAY);
Is it possible to use that function or something similar to be able to pull in a value from another config table to determine how many days you want to go back?
SELECT DATE_SUB(NOW(), INTERVAL x DAY);
This is part of a project to move records from live table to an archive table after a set number of days and I would like to be able to set that up as a configureable value.
This is being run as a daily event on a MySQL 5.7 instance that is setup in AWS
Solution
You can try this:
SELECT DATE_SUB(NOW(), INTERVAL (select interval from yourconfigtable) DAY);
Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange