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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.