Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
php-mysql statement COUNT and multiple GROUP BY?
I'm trying to consolidate inventory records in db. (I'm displaying the result in a table form but for this post I'm putting it simple)
table_name= PC:
department - msoffice
IT - 2000
IT - 2002
IT - 2000
SNM - 2000
SNM - 2003
Finance - 2003
-----------------------------------
expected output:
msoffice - dept - qty
2000 - IT - 2
2000 - SNM - 1
2002 - IT - 1
2003 - Finance - 1
2003 - SNM - 1
Group total by msoffice:
2000 = 3 (2+1)
2002 = 1
2003 = 3 (2+2)
Grand Total : 7
----------------------------------
i have tried this method in 2 different queries that give me 2 outputs. 1 displays the Group Total and the other one displays the grouped result.
Query#1:
$qry=mysql_query("SELECT `msoffice`,`department`, COUNT(`msoffice`) as 'qty', COUNT(`department`) as 'dept' FROM pc GROUP BY `department`");
while ($result=mysql_fetch_array($qry)){
echo $result['msoffice']." - " .$result['qty'] ."<BR>";
}
output for query #1:
2000 - 3
2002 - 1
2003 - 2
---------------------------------------------
Query #2:
$qry2=mysql_query("SELECT `msoffice`,`department`, COUNT( `department`) as 'qty' FROM pc GROUP BY `msoffice`,`department`");
while ($result2=mysql_fetch_array($qry2)){
echo $result2['department']." - ' . $result2['msoffice'] . " - " .$result2['qty']. "<BR>";
}
output for query #2:
IT - 2000 - 2
SNM - 2000 - 1
IT - 2002 - 1
Finance - 2003 - 1
SNM - 2003 - 1
--------------------------------------------
problem:
1) ***MAIN Issue*** I don't know how to get the group total by msoffice. And the overall total i think i can use the row count (is there any better way?)
2) Can the 2 query be combined as 1? If can what is the best method (guide me with example)?
You help is greatly appreciated.
sorry for the typo... it should be
Grand Total: 6
2003 = 2
2 Answers
- 1 decade agoFavorite Answer
I'm kinda confused on how you got your expected output... shouldn't there be only 2 2003's? and shouldn't the grand total be only 6? or maybe I'm missing something...
- 4 years ago
you may loop the fetch command in a on an identical time as fact. The mysql_fetch_* instructions will fetch purely one row with each and each call. you ought to create an array into which you push each and each fetched row after it rather is back. whilst the fetching is done, you loop contained in direction of the array of rows and do what you desire with the advice in each physique. you may use the value back from the fetch because of the fact the try. If the fetch returns fake, then there are no longer from now on rows. via the way, I additionally observed which you assigned the two the question variable and the fetch variable to the comparable value ($SelectThis1). no longer good. if it rather is a typo, no project. yet whilst that's the way it rather is coded, you're overwriting the sq. question with the value you fetched. hint: use the array_push function to stick the fetched rows into your new array.