sql Alter table:在2列之间的datediff上创建一个列

时间:2017-08-26 13:51:46

标签: sql hsqldb

我想基于两个现有列(date1和date2)之间的datediff函数创建一个计算列。 (在几天内)

date1和date2是sql DATE类型。

我没有成功的尝试:

const canvas = document.createElement("canvas");
document.body.appendChild(canvas);
const ctx = canvas.getContext('2d');
canvas.width = 400;

ctx.lineWidth = 15;
ctx.lineJoin = "round";
ctx.lineCap = "butt";
ctx.beginPath();
ctx.moveTo(100,20);
ctx.lineTo(150,120);
ctx.lineTo(50,120);
ctx.closePath(); // creates a line from (50,210) to (100,20)
// closed path with the line joined at the start
ctx.stroke()

ctx.beginPath();
ctx.moveTo(250,20);
ctx.lineTo(300,120);
ctx.lineTo(200,120);
ctx.lineTo(250,20);
// path is open the last line back to the start is still open and
// will use ctx.lineCap to render the start and end
ctx.stroke()

感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

当GENERATED列在其他列中引用的值发生更改时,会自动更新。正确的语法是:

ALTER TABLE my_table ADD length INT GENERATED ALWAYS AS (DATEDIFF('day', date1, date2))