sql Alter table: 在 2 列之间基于 datediff 创建一个列
sql Alter table: create a column base on datediff between 2 columns
我想在 2 个现有列(date1 和 date2)之间创建一个基于 datediff 函数的计算列。 (以天为单位)
date1 和 date2 是 sql DATE 类型。
我尝试过但没有成功:
ALTER TABLE my_table ADD lenght AS datediff('dd', date1, date2)
感谢您的帮助。
ALTER TABLE my_table ADD lenght AS int;
UPDATE my_table SET lenght = DateDiff('dd', date1, date2);
-- Don't forget to add a trigger that fires on updated and inserted rows that will keep the value of lenght valid if the date1 or date2 changes
当 GENERATED 列在其他列中引用的值发生变化时,它会自动更新。正确的语法是:
ALTER TABLE my_table ADD length INT GENERATED ALWAYS AS (DATEDIFF('day', date1, date2))
我想在 2 个现有列(date1 和 date2)之间创建一个基于 datediff 函数的计算列。 (以天为单位)
date1 和 date2 是 sql DATE 类型。
我尝试过但没有成功:
ALTER TABLE my_table ADD lenght AS datediff('dd', date1, date2)
感谢您的帮助。
ALTER TABLE my_table ADD lenght AS int;
UPDATE my_table SET lenght = DateDiff('dd', date1, date2);
-- Don't forget to add a trigger that fires on updated and inserted rows that will keep the value of lenght valid if the date1 or date2 changes
当 GENERATED 列在其他列中引用的值发生变化时,它会自动更新。正确的语法是:
ALTER TABLE my_table ADD length INT GENERATED ALWAYS AS (DATEDIFF('day', date1, date2))