SQL-在列中添加值

时间:2012-02-17 20:07:11

标签: mysql sql

如何在SQL列中添加值?我在xampp中设置了表格,我正在尝试将标题为“粗略”的列中的所有值相加。

3 个答案:

答案 0 :(得分:12)

SQL Server或MySQL:

select sum(MyColumn) as MyColumnSum from MyTable

如果您需要按照其他列的分组对列进行总结

select sum(MyColumn) as MyColumnSum, OtherColumn from MyTable Group By OtherColumn

这是一种单独添加负数或正数的方法

select
  sum( case when MyColumn < 0 then MyColumn else 0 end ) as NegativeSum,
  sum( case when MyColumn > 0 then MyColumn else 0 end ) as PositiveSum
from
  MyTable

<强>参考

答案 1 :(得分:1)

select sum(yourCol) as Gross
from YourTable

使用聚合函数SUM()。

答案 2 :(得分:1)

查看SUM() function documentation for MySQL

SELECT    YourRecordID,
          SUM(Gross) AS GrossSum
FROM      YourTable
GROUP BY  YourRecordID