Jump to content

Sum of Count


BoarderLine

Recommended Posts

Hi.  Is it possible to get the total sum of all count values returned in a query (not total rows of the query but the total sum of a count), but have it returned as a separate variable within the query?.

 

If by using the following query:

SELECT COUNT(a.field) AS number FROM a WHERE a.field='1' 

 

it returns:

6
7
9

 

Is it possible to somehow place a variable within the query that will give the value:

22

from the above example?

Link to comment
https://forums.phpfreaks.com/topic/240568-sum-of-count/
Share on other sites

I can get the result I need with:-

SELECT COUNT(IF(a.field='1',1,NULL)) AS number, b.name FROM b LEFT JOIN a ON a.id = b.id GROUP BY b.name WITH ROLLUP 

 

Which results:-

henry    6
fred     7
harry    9  
terry    0
NULL     22

 

I can then get the ROLLUP value by using mysql_data_seek() however this makes the rest of the dataset unusable later on the page.  I think I may have to move this now to the php forum, unless anyone can share a work around??  Thanks.

Link to comment
https://forums.phpfreaks.com/topic/240568-sum-of-count/#findComment-1236111
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.