以下是语法-
alter table yourTableName add column yourColumnName yourDataType generated always as ((yourColumName1+yourColumName2+....N) / N) virtual;
让我们创建一个表-
mysql> create table demo32 −> ( −> value1 int, −> value2 int −> );
借助insert命令将一些记录插入表中-
mysql> insert into demo32 values(30,60); mysql> insert into demo32 values(20,40); mysql> insert into demo32 values(35,35);
使用select语句显示表中的记录-
mysql> select *from demo32;
这将产生以下输出-
+--------+--------+ | value1 | value2 | +--------+--------+ | 30 | 60 | | 20 | 40 | | 35 | 35 | +--------+--------+ 3 rows in set (0.00 sec)
以下是查询表(创建/更改),以便在不使用INSERT,UPDATE的情况下查询整个表时显示计算出的“平均得分”字段-
mysql> alter table demo32 add column `Average Score` float −> generated always as ((value1+value2) / 2) virtual; Records: 0 Duplicates: 0 Warnings: 0
使用select语句显示表中的记录-
mysql> select *from demo32;
这将产生以下输出-
+--------+--------+---------------+ | value1 | value2 | Average Score | +--------+--------+---------------+ | 30 | 60 | 45 | | 20 | 40 | 30 | | 35 | 35 | 35 | +--------+--------+---------------+ 3 rows in set (0.00 sec)