Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

summing the sum in mysql

Tags:

mysql

Say If I get a resultset in mysql as :

ID  count(posts)
101 2344
102 3245
103 232
104 23

Is there any way to get the tota count of count(posts) in the query itself, like this?

ID  count(posts)
101 2344
102 3245
103 232
104 23
   ------
    5844
like image 972
JPro Avatar asked May 25 '10 12:05

JPro


People also ask

How do I sum two columns in MySQL?

SELECT CONCAT('SELECT ', group_concat(`COLUMN_NAME` SEPARATOR '+'), ' FROM scorecard') FROM `INFORMATION_SCHEMA`. `COLUMNS` WHERE `TABLE_SCHEMA` = (select database()) AND `TABLE_NAME` = 'scorecard' AND `COLUMN_NAME` LIKE 'mark%'; The query above will generate another query that will do the selecting for you.

How do I sum total in SQL?

If you need to add a group of numbers in your table you can use the SUM function in SQL. This is the basic syntax: SELECT SUM(column_name) FROM table_name; If you need to arrange the data into groups, then you can use the GROUP BY clause.

How can I sum two rows in MySQL?

SUM() function. MySQL SUM() function returns the sum of an expression. SUM() function returns NULL when the return set has no rows.


1 Answers

SELECT  id, COUNT(*)
FROM    mytable
GROUP BY
        id WITH ROLLUP
like image 129
Quassnoi Avatar answered Sep 22 '22 03:09

Quassnoi