mysql count the sum of all rows

喜夏-厌秋 提交于 2019-11-27 17:38:25

问题


I have a mysql table that has a number of rows, and in each row a field called "value", the field value will differ from row to row. What I want, is to select all the rows and count the sum of all the "value" fields.

any idea?


回答1:


Do you mean like this?

SELECT    SUM(value)
FROM      myTable

If you have multiple columns to return, simply add each non-aggregate (i.e., summed) row to the GROUP BY clause:

SELECT    firstName, lastName, SUM(value)
FROM      myTable
GROUP BY  firstName, lastName



回答2:


SELECT SUM(value) as total FROM table;

$row['total'];



回答3:


SELECT SUM(`value`) FROM `your_table`



回答4:


SELECT SUM(value)
    FROM YourTable



回答5:


What you'll want is the GROUP-function named SUM.




回答6:


This query will return the sum of value and the number of rows count:

SELECT count(*), sum(value) FROM tablename


来源:https://stackoverflow.com/questions/6131197/mysql-count-the-sum-of-all-rows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!