BoarderLine Posted June 27, 2011 Share Posted June 27, 2011 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 More sharing options...
requinix Posted June 27, 2011 Share Posted June 27, 2011 How are you getting multiple rows in the resultset? There should only be one: the number of rows in the table that have a.field=1. Link to comment https://forums.phpfreaks.com/topic/240568-sum-of-count/#findComment-1235701 Share on other sites More sharing options...
BoarderLine Posted June 28, 2011 Author Share Posted June 28, 2011 Of course, sorry. What I meant to put as example query was this: SELECT COUNT(IF(a.field='1',1,NULL)) AS number, b.name FROM b LEFT JOIN a ON a.id = b.id example result: henry 6 fred 7 harry 9 terry 0 Link to comment https://forums.phpfreaks.com/topic/240568-sum-of-count/#findComment-1235715 Share on other sites More sharing options...
silkfire Posted June 28, 2011 Share Posted June 28, 2011 Just do: SELECT SUM(number) Link to comment https://forums.phpfreaks.com/topic/240568-sum-of-count/#findComment-1236007 Share on other sites More sharing options...
BoarderLine Posted June 29, 2011 Author Share Posted June 29, 2011 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 More sharing options...
BoarderLine Posted June 29, 2011 Author Share Posted June 29, 2011 And sorry forgot the essentials: MySQL 4.1.47 Link to comment https://forums.phpfreaks.com/topic/240568-sum-of-count/#findComment-1236163 Share on other sites More sharing options...
silkfire Posted June 29, 2011 Share Posted June 29, 2011 Why don't you just sum it with PHP? Link to comment https://forums.phpfreaks.com/topic/240568-sum-of-count/#findComment-1236180 Share on other sites More sharing options...
BoarderLine Posted June 29, 2011 Author Share Posted June 29, 2011 Thanks silkfire, The problem is I am looping through the data set in a list but need the total sum of 'counter' for the whole data set before the result list is populated. I am trying to avoid doing this in two queries to reduce processing time. Link to comment https://forums.phpfreaks.com/topic/240568-sum-of-count/#findComment-1236253 Share on other sites More sharing options...
silkfire Posted June 29, 2011 Share Posted June 29, 2011 Easy then. Loop thru the result set and then save each value into one array. Then use array_sum to print the sum and loop thru the array to print each individual's name and score. Link to comment https://forums.phpfreaks.com/topic/240568-sum-of-count/#findComment-1236256 Share on other sites More sharing options...
BoarderLine Posted June 29, 2011 Author Share Posted June 29, 2011 Ta mate. Saved me. Cheers Link to comment https://forums.phpfreaks.com/topic/240568-sum-of-count/#findComment-1236366 Share on other sites More sharing options...
BoarderLine Posted June 29, 2011 Author Share Posted June 29, 2011 And obviously this way the WITH ROLLUP was not needed. Link to comment https://forums.phpfreaks.com/topic/240568-sum-of-count/#findComment-1236367 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.