两列的总和并显示在第三列中

时间:2015-01-15 07:49:45

标签: sql

我在SQL表中有三列,

我想显示第三列中两列总和的总和。如何显示使用SQL查询。

这是我的表结构

Id  int Unchecked
Col_A    int    Unchecked
Col_B    int    Unchecked
Total     int   Checked

5 个答案:

答案 0 :(得分:3)

无需在表中存储总数,您只需将其作为SQL查询的一部分进行计算,如下所示:

SELECT
    Id,
    Col_A,
    Col_B,
    Col_A + Col_B AS Total
FROM tablename

答案 1 :(得分:1)

您可以使用计算列:

CREATE TABLE [dbo].[Test](
    [a] [INT] NULL,
    [b] [INT] NULL,
    [c]  AS ([a]+[b])
) ON [PRIMARY]

GO

INSERT INTO dbo.Test
        ( a, b )
VALUES  ( 1, -- a - int
          2  -- b - int
          )


SELECT * FROM dbo.Test 

Results:
a   b   c
1   2   3

答案 2 :(得分:0)

这是我的表格,其中colums是薪水和变量。

enter image description here

现在我需要基本和变量之和作为总数。 以下是对此的查询 -

select basic,variable,(basic+variable) as total from salary

enter image description here

答案 3 :(得分:0)

This is the given table:

After performing the following query

enter image description here

或者如果您只想显示总计:

enter image description here

答案 4 :(得分:-1)

尝试以下-

ALTER TABLE TABLENAME ADD colC AS ColA * ColB