I am very new to MySQL and db's in general.
I have a table where I have date_of_test
and test_frequency
(Possible values 3,6,12).
I need to have a field future_test
that has the calculated date in it derived from date_of_test
and the value in test_frequency
which is either 3,6,12 (Months).
I do not know how to achieve this, could somebody point me in the right direction?
alter table gypsum add column future_test (datetime);
update gypsum
set future_test = date_add(date_of_test, interval test_frequency MONTH);
As pointed in comments, it's often not a great idea to have calculated values in a database.
You'll have to relaunch the calculation each time you update a date_or_test, a test_frequency, or each time you insert a new value...
It might be better to create a dedicated view
CREATE OR REPLACE VIEW gypsumWithFuture AS
(SELECT
id,
date_of_test,
test_frequency,
DATE_ADD(date_of_test, interval test_frequency MONTH) AS future_test
FROM gypsum)
or to do the calculation in your application's code (if you have one). A wide range of langagues has DateTime function, which can do the same as MySQL DATE_ADD .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.