我可以使用 MySQL 中的 SUM()
函数轻松选择一列的总和,如下所示:
SELECT SUM(`someColumn`) FROM `table`
是否也可以使用相同的查询返回第二列的总和?类似这样:
SELECT SUM (`someColumn`, `anotherColumn`) FROM `table`
运行这样的查询会返回以下访问冲突:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' `anotherColumn`) FROM `table`' at line 1
最佳答案
尝试:
SELECT SUM(COALESCE(`someColumn`, 0)+ COALESCE(`anotherColumn`, 0)) as TotalSum FROM `table`
或者如果你想单独使用它们:
SELECT SUM(`someColumn`) as Sum1, SUM(`anotherColumn`) as Sum2 FROM `table`
关于mysql - 在 MySQL 中选择两个柱状和?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20739662/