简体   繁体   English

在SQL列中添加合计其他列值的值

[英]Adding in SQL Column that Totals Other Column Values

Trying to add a new column into pre-existing table with the following command: 尝试使用以下命令将新列添加到现有表中:

ALTER TABLE randomquestions 
ADD Total AS R1_Number + R2_Number + R3_Number + R4_Number PERSISTED

I get this error: 我收到此错误:

#1064 - You have an error in your SQL syntax; #1064-您的SQL语法有误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AS R1_Number + R2_Number + R3_Number + R4_Number PERSISTED' at line 2 请查看与您的MySQL服务器版本相对应的手册,以在第2行的'AS R1_Number + R2_Number + R3_Number + R4_Number PERSISTED'附近使用正确的语法

Can anyone tell me where I went wrong with the syntax? 谁能告诉我语法哪里出问题了?

If MySQL 5.7.6+ Generated Columns : 如果MySQL 5.7.6+ 生成的列

ALTER TABLE randomquestions 
ADD Total INT AS (R1_Number + R2_Number + R3_Number + R4_Number) STORED;

Note that you may need to change datatype depending of datatype of Rx_Number . 请注意,您可能需要根据Rx_Number的数据类型来更改数据类型。

Alter looks good missing parenthesis and column type. Alter看起来很好,缺少括号和列类型。

ALTER TABLE randomquestions 
ADD Total int AS (R1_Number + R2_Number + R3_Number + R4_Number) PERSISTED

I tried a similar statement with the parenthesis and it worked: 我用括号尝试了一个类似的语句,它起作用了:

use test
alter table [dbo].[tablename]
Add total2 as (price * 2) persisted 

(so I guess it's the parenthesis, it's not finding your table, or just a syntax error with the field names.) (所以我想这是括号,它不是在查找表,或者只是字段名称的语法错误。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM