减去两列值并将结果存储在另一列中 [英] subtract two column values and store result in another column

查看:56
本文介绍了减去两列值并将结果存储在另一列中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PLZ任何人都可以帮助我,我有一个表格,例如tbl_1,我有列,例如ABC

,当我插入A列和B列时,其结果存储到C中,如c = ab

plz can anybody help me i have the one table eg tbl_1 and i have column eg A B C
when i insert into column A and B its result store into C like c=a-b

推荐答案

这可以通过多种方式完成。您可以定义一个触发器来进行计算并存储结果,或者您根本不能存储结果,而是创建一个可以进行计算的视图。但也许最简单的解决方案是使用计算列。



请考虑以下内容

This could be done in several ways. You could define a trigger making the calculation and storing the result or you could not store the result at all but create a view which would do the calculation. But perhaps the simplest solution would be to use a computed column.

Consider the following
CREATE TABLE CalcTest (
   A int,
   B int,
   C AS A-B
);

INSERT INTO CalcTest (A, B) VALUES (2,1);
INSERT INTO CalcTest (A, B) VALUES (1,1);
INSERT INTO CalcTest (A, B) VALUES (1,2);
INSERT INTO CalcTest (A, B) VALUES (NULL,1);
INSERT INTO CalcTest (A, B) VALUES (1,NULL);
INSERT INTO CalcTest (A, B) VALUES (NULL,NULL);

SELECT * FROM CalcTest;



SELECT查询的结果将是


The result of the SELECT query would be

A      B      C
----   ----   ----
2      1      1
1      1      0
1      2      -1
NULL   1      NULL
1      NULL   NULL
NULL   NULL   NULL


这篇关于减去两列值并将结果存储在另一列中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆