SQL:新列中2列的差异
SQL: difference of 2 columns in a new column
我有以下 SQL table,有一个 positive
列和一个 negative
列,都是 int
。
positive negative
----------------------
5 3
3 1
10 7
如何创建第三列,例如total
等于 positive - negative
。此外,我希望每次 positive
或 negative
列的元素更改时更新 total
列。
如何在 SQL 中执行此操作?
编辑:我正在使用 MariaDB
按照此处所述使用虚拟计算列https://mariadb.com/kb/en/mariadb/virtual-computed-columns/
create table table1
(
positive int not null,
negative int not null,
difference int as (positive - negative) virtual
);
如下所述使用计算列
您可以将 table 创建为
create table table_name ( positive int, negitive int, difference as positive-negitive)
然后在创建之后,如果您输入值
insert into table_name values(3,2)
--无需输入第三列,称为计算列。
然后插入后差异将出现在第三列"difference"
我有以下 SQL table,有一个 positive
列和一个 negative
列,都是 int
。
positive negative
----------------------
5 3
3 1
10 7
如何创建第三列,例如total
等于 positive - negative
。此外,我希望每次 positive
或 negative
列的元素更改时更新 total
列。
如何在 SQL 中执行此操作?
编辑:我正在使用 MariaDB
按照此处所述使用虚拟计算列https://mariadb.com/kb/en/mariadb/virtual-computed-columns/
create table table1
(
positive int not null,
negative int not null,
difference int as (positive - negative) virtual
);
如下所述使用计算列
您可以将 table 创建为
create table table_name ( positive int, negitive int, difference as positive-negitive)
然后在创建之后,如果您输入值
insert into table_name values(3,2)
--无需输入第三列,称为计算列。
然后插入后差异将出现在第三列"difference"